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 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
Stone Smashing = Partition ProblemGroup AGroup B7124Sum = 8Sum = 6Difference = |8 - 6| = 2But we can do better!Try: Group A = {7} (sum=7), Group B = {2,4,1} (sum=7)Perfect balance! Difference = 0DP helps us find the subset sum closest to total/2 efficiently
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

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

Recursion stack depth for generating subsets

n
2n
โšก 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
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
42.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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