Put Marbles in Bags - Problem
Marble Distribution Challenge
You're managing a marble collection with
Distribution Rules:
• Every bag must contain at least one marble
• Marbles must be placed in contiguous segments - if marbles at positions i and j are in the same bag, all marbles between them must also be in that bag
• Each bag's cost is calculated as:
Your Mission: Find the difference between the maximum possible total cost and minimum possible total cost across all valid distributions.
Example: With marbles
• Split [1] | [3,5,1]: cost = (1+1) + (3+1) = 6
• Split [1,3] | [5,1]: cost = (1+3) + (5+1) = 10
• Split [1,3,5] | [1]: cost = (1+5) + (1+1) = 8
Maximum cost (10) - Minimum cost (6) = 4
You're managing a marble collection with
k specialized storage bags. Each marble has a specific weight, and you must distribute all marbles according to strict rules.Distribution Rules:
• Every bag must contain at least one marble
• Marbles must be placed in contiguous segments - if marbles at positions i and j are in the same bag, all marbles between them must also be in that bag
• Each bag's cost is calculated as:
weight[first_marble] + weight[last_marble]Your Mission: Find the difference between the maximum possible total cost and minimum possible total cost across all valid distributions.
Example: With marbles
[1,3,5,1] and k=2 bags:• Split [1] | [3,5,1]: cost = (1+1) + (3+1) = 6
• Split [1,3] | [5,1]: cost = (1+3) + (5+1) = 10
• Split [1,3,5] | [1]: cost = (1+5) + (1+1) = 8
Maximum cost (10) - Minimum cost (6) = 4
Input & Output
example_1.py — Basic Case
$
Input:
weights = [1,3,5,1], k = 2
›
Output:
4
💡 Note:
The optimal distributions are: Min cost: [1] | [3,5,1] with cost (1+1) + (3+1) = 6. Max cost: [1,3] | [5,1] with cost (1+3) + (5+1) = 10. Difference: 10 - 6 = 4.
example_2.py — Three Bags
$
Input:
weights = [1,3,5,1], k = 3
›
Output:
4
💡 Note:
With 3 bags, possible distributions: [1] | [3] | [5,1] = (1+1) + (3+3) + (5+1) = 14, [1] | [3,5] | [1] = (1+1) + (3+5) + (1+1) = 18, [1,3] | [5] | [1] = (1+3) + (5+5) + (1+1) = 20. Max: 20, Min: 14, Difference: 6.
example_3.py — Edge Case
$
Input:
weights = [1,1,1,1], k = 2
›
Output:
0
💡 Note:
All marbles have the same weight, so all possible distributions yield the same cost. Any split like [1] | [1,1,1] gives cost (1+1) + (1+1) = 4. The difference between max and min is 0.
Constraints
- 1 ≤ k ≤ weights.length ≤ 105
- 1 ≤ weights[i] ≤ 109
- No bag can be empty
- Marbles must be placed in contiguous segments
Visualization
Tap to expand
Understanding the Visualization
1
Identify Boundaries
Each possible cut position between adjacent marbles has a cost equal to the sum of the two marbles it separates
2
Sort Options
Sort all possible boundary costs to identify the cheapest and most expensive cuts
3
Choose Optimally
For minimum cost, choose the k-1 cheapest cuts. For maximum cost, choose the k-1 most expensive cuts
4
Calculate Difference
The answer is the difference between the maximum and minimum possible total costs
Key Takeaway
🎯 Key Insight: The problem reduces to selecting k-1 optimal boundary cuts. By sorting all possible cut costs and choosing the cheapest for minimum and most expensive for maximum, we achieve optimal O(n log n) performance.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code