Last Stone Weight II - Problem
Last Stone Weight II is a fascinating optimization problem that challenges you to think strategically about stone smashing!
Imagine you're playing a destructive game with stones of different weights. In each turn, you pick any two stones and smash them together. Here's what happens:
โข If both stones have the same weight โ Both stones are completely destroyed! ๐ฅ
โข If they have different weights โ The lighter stone is destroyed, and the heavier stone survives but loses weight equal to the lighter stone
For example, if you smash stones of weight
Your goal is to find the smallest possible weight of the final remaining stone after all possible smashing is done. If no stones remain, return
The key insight: This isn't just about randomly smashing stones โ there's an optimal strategy hidden within!
Imagine you're playing a destructive game with stones of different weights. In each turn, you pick any two stones and smash them together. Here's what happens:
โข If both stones have the same weight โ Both stones are completely destroyed! ๐ฅ
โข If they have different weights โ The lighter stone is destroyed, and the heavier stone survives but loses weight equal to the lighter stone
For example, if you smash stones of weight
7 and 3, the stone of weight 3 disappears, and you're left with a stone of weight 7 - 3 = 4.Your goal is to find the smallest possible weight of the final remaining stone after all possible smashing is done. If no stones remain, return
0.The key insight: This isn't just about randomly smashing stones โ there's an optimal strategy hidden within!
Input & Output
example_1.py โ Basic Case
$
Input:
[2,7,4,1]
โบ
Output:
0
๐ก Note:
We can arrange stones optimally: Combine 2+4=6 and 7+1=8, then combine 6 and 8 to get |8-6|=2. But better: combine stones to get two groups [7,1] and [2,4] with sums 8 and 6, difference is 2. Actually optimal is [7+1] vs [4+2] = 8 vs 6 = difference 2. Wait, even better: we can achieve perfect balance with subset [7] = 7 and remaining [2,4,1] = 7, so difference is 0!
example_2.py โ Odd Sum
$
Input:
[31,26,33,21,40]
โบ
Output:
1
๐ก Note:
Total sum = 151. We need to find subset closest to 151/2 = 75.5. The best we can do is find a subset with sum 75, leaving remainder with sum 76. Difference = |76-75| = 1.
example_3.py โ Single Stone
$
Input:
[1]
โบ
Output:
1
๐ก Note:
With only one stone, no smashing is possible. The final weight is the stone's original weight: 1.
Visualization
Tap to expand
Understanding the Visualization
1
Reframe the problem
Instead of simulating stone smashing, think about dividing stones into two groups
2
Find balanced partition
The goal is to make the two groups have sums as close as possible
3
Use DP for subset sum
Dynamic programming efficiently finds which subset sums are achievable
4
Calculate final difference
The minimum remaining weight is |total - 2 ร best_sum|
Key Takeaway
๐ฏ Key Insight: Transform the stone smashing simulation into a balanced partition problem - much more efficient to solve!
Time & Space Complexity
Time Complexity
O(2^n)
We generate all 2^n possible subsets of stones
โ Quadratic Growth
Space Complexity
O(n)
Recursion stack depth for generating subsets
โก Linearithmic Space
Constraints
- 1 โค stones.length โค 30
- 1 โค stones[i] โค 100
- Key insight: This is equivalent to partitioning stones into two groups with minimum difference
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code