Last Stone Weight II
Last Stone Weight II is LeetCode 1049 (Medium). You get an array stones of positive weights. Each turn, pick any two stones with weights x <= y and smash them together:
- If
x == y, both stones are destroyed. - Otherwise
xis destroyed andybecomesy - x.
Keep going until at most one stone is left. Return the smallest possible weight of that stone, or 0 if no stone is left.
There are at most 30 stones, each weighing at most 100, so the total weight is at most 3,000. That small total is what makes a table over sums affordable.
- 1 <= stones.length <= 30
- 1 <= stones[i] <= 100
Intuition
Each time a stone is smashed, its weight is either added to the survivor or taken away from it. So the last stone always equals (sum of one group of stones) − (sum of the rest), for some way of splitting the stones into two piles.
With total S and one pile summing to s, the last stone is S − 2s. To make it small, make s as close to S / 2 as possible without going over.
That is a subset sum question: which pile sums up to S / 2 can you build from the stones? A table of yes/no values over those sums answers it, and the best reachable sum gives the answer.
"Split items into two groups so the group sums are as close as possible" is subset sum with target half the total. Partition Equal Subset Sum asks whether the gap can be 0, Target Sum counts the ways to place signs, and this problem asks for the smallest gap.
Approach
Before reading on, smash [31, 26, 33, 21, 40] by always taking the two heaviest stones. Then try to split the same stones into two piles with sums as close as you can. Which gives the smaller last stone?
Aim one pile at half the total
Compute S = sum(stones) and half = S // 2. The smaller pile can never be more than half, so only sums from 0 to half matter, and the answer is S - 2 * best for the largest reachable best.
Mark which sums can be made
Make a list dp of half + 1 booleans, where dp[t] means some group of stones sums to exactly t. Set dp[0] = True, because a pile with no stones sums to 0.
Add stones one by one, sums high to low
For each stone w, go through t from half down to w and set dp[t] = True when dp[t - w] is true. Going downward means dp[t - w] still describes the stones before w, so each stone is used at most once.
Take the largest reachable sum
Scan t down from half and stop at the first dp[t] that is true. That pile is as close to half as the stones allow, and S - 2 * t is the smallest last stone.
Code in Python, C++ and Java
Common pitfalls
Playing the game greedily with a heap
smash the two heaviest stones each turn
split the stones into two piles closest to half
Here you choose the pairs, and the greedy choice is not the best one. On [31, 26, 33, 21, 40] it leaves 9, while the piles {40, 33} and {31, 26, 21} (73 and 78) leave 5.
Looping the sums upward
for t in range(w, half + 1):
for t in range(half, w - 1, -1):
Going upward, a sum just made with w is read again in the same pass, so one stone counts twice. On [1, 4] the sum 2 looks reachable as 1 + 1, giving 1 instead of the correct 3.
Edge cases
[5]half is 2 and only sum 0 is reachable, so the answer is 5 - 0 = 5: nothing can be smashed.
[3, 3, 2, 4]Half of 12 is 6, and {3, 3} makes it, so the answer is 0 and no stone is left.
Complexity
Last Stone Weight II FAQ
How is it different from Last Stone Weight?
In Last Stone Weight you must always smash the two heaviest stones, so a heap simply plays the game out. Here you pick the pairs, so the question becomes a subset sum DP over pile sums.
Can every split into two piles really be reached by smashing?
For the best split, yes. Smash a stone from the heavier pile against one from the lighter pile and return any leftover to its own pile: both pile sums drop by the same amount, so their gap stays fixed. Repeat until the lighter pile is empty. If the heavier pile still held two or more stones, moving one of them (with the original stones it came from) across at the start would have given a smaller gap, which the best split does not allow. So exactly one stone, of weight equal to the gap, is left.