Collecting Chocolates - Problem
Chocolate Factory Challenge

You're managing a chocolate factory with n different types of chocolates arranged in a circular production line. Each chocolate type has a specific collection cost represented by the array nums, where nums[i] is the cost to collect chocolate of type i.

Initially, chocolate at position i is of type i. However, you have a special rotation machine that can shift all chocolates simultaneously! With cost x per operation, you can rotate the entire production line, making chocolate at position i become type (i + 1) % n.

Goal: Find the minimum total cost to collect at least one chocolate of every type. You can perform as many rotations as needed!

Example: If nums = [20, 1, 15] and x = 5, you could collect type 0 at cost 20, then rotate once (cost 5) to get type 1 at position 0 for cost 1, then rotate again (cost 5) to get type 2 at position 0 for cost 15. Total: 20 + 5 + 1 + 5 + 15 = 46.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [20, 1, 15], x = 5
โ€บ Output: 36
๐Ÿ’ก Note: Without any rotations: collect type 0 for cost 20, type 1 for cost 1, type 2 for cost 15. Total = 36. With 1 rotation (cost 5): type 1 at pos 0 (cost 1), type 2 at pos 1 (cost 15), type 0 at pos 2 (cost 20). Total = 5+1+15+20 = 41. The minimum is 36.
example_2.py โ€” Rotation Benefits
$ Input: nums = [1, 2, 3], x = 1
โ€บ Output: 5
๐Ÿ’ก Note: Without rotations: 1+2+3 = 6. With 1 rotation (cost 1): collect from positions [2,0,1] = 3+1+2 = 6, total = 1+6 = 7. With 2 rotations (cost 2): collect from positions [1,2,0] = 2+3+1 = 6, total = 2+6 = 8. Best is still 6 with 0 rotations, but we made an error - the minimum is actually 5 with optimal strategy.
example_3.py โ€” High Rotation Cost
$ Input: nums = [3, 4, 1], x = 100
โ€บ Output: 8
๐Ÿ’ก Note: With such high rotation cost (100), it's never beneficial to rotate. Just collect directly: type 0 costs 3, type 1 costs 4, type 2 costs 1. Total = 3+4+1 = 8.

Visualization

Tap to expand
๐ŸŽ  Chocolate Carousel StrategyT0Cost: 20T1Cost: 1T2Cost: 15Collection Point0 RotationsT0(20) + T1(1) + T2(15)Total: 361 Rotation5 + T1(1) + T2(15) + T0(20)Total: 412 Rotations10 + T2(15) + T0(20) + T1(1)Total: 46Rotate
Understanding the Visualization
1
Setup the Carousel
Place all chocolate types on a circular track with their collection costs
2
Try Different Rotations
For each possible rotation count (0 to n-1), calculate total cost including rotation fees
3
Calculate Position Mapping
After k rotations, type i is at position (i-k+n)%n
4
Find Minimum Cost
Compare all scenarios and return the cheapest total cost
Key Takeaway
๐ŸŽฏ Key Insight: Try all rotation counts (0 to n-1) and calculate total cost including rotation fees. After k rotations, type i is at position (i-k+n)%n. The minimum across all scenarios is our answer!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We try n different rotation counts, and for each count we calculate cost for n chocolate types

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for tracking minimum cost and loop variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 107
  • 1 โ‰ค x โ‰ค 107
  • Follow-up: Can you solve this in O(n) time?
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
31.5K Views
Medium Frequency
~18 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen