Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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.
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] <= 1091 <= x <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[20,1,15] 5
[1,2,3] 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2735: Collecting Chocolates
class Solution {
public long minCost(int[] nums, int x) {
int n = nums.length;
int[][] f = new int[n][n];
for (int i = 0; i < n; ++i) {
f[i][0] = nums[i];
for (int j = 1; j < n; ++j) {
f[i][j] = Math.min(f[i][j - 1], nums[(i - j + n) % n]);
}
}
long ans = 1L << 60;
for (int j = 0; j < n; ++j) {
long cost = 1L * x * j;
for (int i = 0; i < n; ++i) {
cost += f[i][j];
}
ans = Math.min(ans, cost);
}
return ans;
}
}
// Accepted solution for LeetCode #2735: Collecting Chocolates
func minCost(nums []int, x int) int64 {
n := len(nums)
f := make([][]int, n)
for i, v := range nums {
f[i] = make([]int, n)
f[i][0] = v
for j := 1; j < n; j++ {
f[i][j] = min(f[i][j-1], nums[(i-j+n)%n])
}
}
ans := 1 << 60
for j := 0; j < n; j++ {
cost := x * j
for i := 0; i < n; i++ {
cost += f[i][j]
}
ans = min(ans, cost)
}
return int64(ans)
}
# Accepted solution for LeetCode #2735: Collecting Chocolates
class Solution:
def minCost(self, nums: List[int], x: int) -> int:
n = len(nums)
f = [[0] * n for _ in range(n)]
for i, v in enumerate(nums):
f[i][0] = v
for j in range(1, n):
f[i][j] = min(f[i][j - 1], nums[(i - j) % n])
return min(sum(f[i][j] for i in range(n)) + x * j for j in range(n))
// Accepted solution for LeetCode #2735: Collecting Chocolates
impl Solution {
pub fn min_cost(nums: Vec<i32>, x: i32) -> i64 {
let n = nums.len();
let mut f = vec![vec![0; n]; n];
for i in 0..n {
f[i][0] = nums[i];
for j in 1..n {
f[i][j] = f[i][j - 1].min(nums[(i - j + n) % n]);
}
}
let mut ans = i64::MAX;
for j in 0..n {
let mut cost = (x as i64) * (j as i64);
for i in 0..n {
cost += f[i][j] as i64;
}
ans = ans.min(cost);
}
ans
}
}
// Accepted solution for LeetCode #2735: Collecting Chocolates
function minCost(nums: number[], x: number): number {
const n = nums.length;
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
for (let i = 0; i < n; ++i) {
f[i][0] = nums[i];
for (let j = 1; j < n; ++j) {
f[i][j] = Math.min(f[i][j - 1], nums[(i - j + n) % n]);
}
}
let ans = Infinity;
for (let j = 0; j < n; ++j) {
let cost = x * j;
for (let i = 0; i < n; ++i) {
cost += f[i][j];
}
ans = Math.min(ans, cost);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.