Collecting Chocolates
MediumUpdated: Jul 7, 2025
Practice on:
Problem
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
ithtype to((i + 1) mod n)thtype 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.
Examples
Example 1
Input: nums = [20,1,15], x = 5
Output: 13
Explanation: 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 this is optimal.
Example 2
Input: nums = [1,2,3], x = 4
Output: 6
Explanation: We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
Constraints
1 <= nums.length <= 10001 <= nums[i] <= 10^91 <= x <= 10^9
Solution
Method 1 – Enumerate Rotations (Sliding Minimum)
Intuition
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.
Approach
- Let
nbe the length ofnums. - For each rotation
kfrom 0 to n-1:- For each type, update the minimum cost to collect that type after
krotations. - Compute the total cost as the sum of minimum costs for all types plus
k * x(the cost of rotations). - Track the minimum total cost.
- For each type, update the minimum cost to collect that type after
- Return the minimum total cost found.
Code
C++
class Solution {
public:
long long minCost(vector<int>& nums, int x) {
int n = nums.size();
vector<long long> minCost(nums.begin(), nums.end());
long long ans = accumulate(minCost.begin(), minCost.end(), 0LL);
for (int k = 1; k < n; ++k) {
for (int i = 0; i < n; ++i)
minCost[i] = min(minCost[i], (long long)nums[(i + k) % n]);
long long total = accumulate(minCost.begin(), minCost.end(), 0LL) + (long long)k * x;
ans = min(ans, total);
}
return ans;
}
};
Go
func minCost(nums []int, x int) int64 {
n := len(nums)
minCost := make([]int, n)
copy(minCost, nums)
ans := int64(0)
for _, v := range minCost {
ans += int64(v)
}
for k := 1; k < n; k++ {
for i := 0; i < n; i++ {
if nums[(i+k)%n] < minCost[i] {
minCost[i] = nums[(i+k)%n]
}
}
total := int64(k) * int64(x)
for _, v := range minCost {
total += int64(v)
}
if total < ans {
ans = total
}
}
return ans
}
Java
class Solution {
public long minCost(int[] nums, int x) {
int n = nums.length;
long[] minCost = new long[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;
}
}
Kotlin
class Solution {
fun minCost(nums: IntArray, x: Int): Long {
val n = nums.size
val minCost = LongArray(n) { nums[it].toLong() }
var ans = minCost.sum()
for (k in 1 until n) {
for (i in 0 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
}
}
Python
class Solution:
def minCost(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
Rust
impl Solution {
pub fn min_cost(nums: Vec<i32>, x: i32) -> i64 {
let n = nums.len();
let mut min_cost: Vec<i64> = nums.iter().map(|&v| v as i64).collect();
let mut ans: i64 = min_cost.iter().sum();
for k in 1..n {
for i in 0..n {
min_cost[i] = min_cost[i].min(nums[(i + k) % n] as i64);
}
let total: i64 = min_cost.iter().sum::<i64>() + (k as i64) * (x as i64);
ans = ans.min(total);
}
ans
}
}
TypeScript
class Solution {
minCost(nums: number[], x: number): number {
const n = nums.length;
let minCost = nums.slice();
let ans = minCost.reduce((a, b) => a + b, 0);
for (let k = 1; k < n; k++) {
for (let i = 0; i < n; i++) {
minCost[i] = Math.min(minCost[i], nums[(i + k) % n]);
}
const total = minCost.reduce((a, b) => a + b, 0) + k * x;
ans = Math.min(ans, total);
}
return ans;
}
}
Complexity
- ⏰ Time complexity: O(n^2)
- 🧺 Space complexity: O(n)