You are given a 0-indexed integer array nums of size n representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index i is nums[i]. Each chocolate is of a different type, and initially, the chocolate at the index i is of ith type.
In one operation, you can do the following with an incurred cost of x:
Simultaneously change the chocolate of ith type to ((i + 1) mod n)th type for all chocolates.
Return the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like.
Input: nums =[20,1,15], x =5Output: 13Explanation: Initially, the chocolate types are [0,1,2]. We will buy the 1st type of chocolate at a cost of 1.Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2nd type of chocolate at a cost of 1.Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0th type of chocolate at a cost of 1.Thus, the total cost will become(1+5+1+5+1)=13. We can prove that thisis optimal.
Input: nums =[1,2,3], x =4Output: 6Explanation: We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is1+2+3=6.
Since each operation rotates the types, we can try all possible numbers of operations (from 0 to n-1). For each rotation, we can keep track of the minimum cost to collect each type so far. The answer is the minimum total cost over all rotations.
classSolution {
publiclongminCost(int[] nums, int x) {
int n = nums.length;
long[] minCost =newlong[n];
for (int i = 0; i < n; i++) minCost[i]= nums[i];
long ans = 0;
for (long v : minCost) ans += v;
for (int k = 1; k < n; k++) {
for (int i = 0; i < n; i++)
minCost[i]= Math.min(minCost[i], nums[(i + k) % n]);
long total = (long)k * x;
for (long v : minCost) total += v;
ans = Math.min(ans, total);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funminCost(nums: IntArray, x: Int): Long {
val n = nums.size
val minCost = LongArray(n) { nums[it].toLong() }
var ans = minCost.sum()
for (k in1 until n) {
for (i in0 until n) {
minCost[i] = minOf(minCost[i], nums[(i + k) % n].toLong())
}
val total = minCost.sum() + k.toLong() * x
ans = minOf(ans, total)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defminCost(self, nums: list[int], x: int) -> int:
n = len(nums)
min_cost = nums[:]
ans = sum(min_cost)
for k in range(1, n):
for i in range(n):
min_cost[i] = min(min_cost[i], nums[(i + k) % n])
total = sum(min_cost) + k * x
ans = min(ans, total)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnmin_cost(nums: Vec<i32>, x: i32) -> i64 {
let n = nums.len();
letmut min_cost: Vec<i64>= nums.iter().map(|&v| v asi64).collect();
letmut ans: i64= min_cost.iter().sum();
for k in1..n {
for i in0..n {
min_cost[i] = min_cost[i].min(nums[(i + k) % n] asi64);
}
let total: i64= min_cost.iter().sum::<i64>() + (k asi64) * (x asi64);
ans = ans.min(total);
}
ans
}
}