Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 1049 Last Stone Weight II - Problem Statement

LeetCode 1049

You have stones with weights. Each turn, pick two stones and smash them. If equal, both destroyed. If not, the lighter one is destroyed and the heavier one becomes xy|x - y|. Return the minimum possible weight of the last stone (or 0 if all destroyed).

This looks like a simulation problem, but simulating all possible smash orders is exponential. There's a mathematical observation that converts this to subset sum. Before reading on, think about what happens when you smash all stones. What's the final result as the original weights?