# LC 1049. Last Stone Weight II ### [Problem link](https://leetcode.com/problems/last-stone-weight-ii/) ###### tags: `leedcode` `python` `c++` `medium` `DP` You are given an array of integers <code>stones</code> where <code>stones[i]</code> is the weight of the <code>ith</code> stone. We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights <code>x</code> and <code>y</code> with <code>x <= y</code>. The result of this smash is: - If <code>x == y</code>, both stones are destroyed, and - If <code>x != y</code>, the stone of weight <code>x</code> is destroyed, and the stone of weight <code>y</code> has new weight <code>y - x</code>. At the end of the game, there is **at most one** stone left. Return the smallest possible weight of the left stone. If there are no stones left, return <code>0</code>. **Example 1:** ``` Input: stones = [2,7,4,1,8,1] Output: 1 Explanation: We can combine 2 and 4 to get 2, so the array converts to [2,7,1,8,1] then, we can combine 7 and 8 to get 1, so the array converts to [2,1,1,1] then, we can combine 2 and 1 to get 1, so the array converts to [1,1,1] then, we can combine 1 and 1 to get 0, so the array converts to [1], then that's the optimal value. ``` **Example 2:** ``` Input: stones = [31,26,33,21,40] Output: 5 ``` **Constraints:** - <code>1 <= stones.length <= 30</code> - <code>1 <= stones[i] <= 100</code> ## Solution 1 - DP #### Python ```python= class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: target = sum(stones) // 2 dp = [0] * (target + 1) for i in range(len(stones)): for j in range(target, stones[i] - 1, -1): dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]) return sum(stones) - dp[-1] * 2 ``` #### C++ ```cpp= class Solution { public: int lastStoneWeightII(vector<int>& stones) { int sum = accumulate(stones.begin(), stones.end(), 0); int target = sum / 2; vector<int> dp(target + 1, 0); for (int i = 0; i < stones.size(); i++) { for (int j = target; j >= stones[i]; j--) { dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]); } } return sum - dp[target] - dp[target]; } }; ``` >### Complexity >m = int(sum(stones) / 2) >n = stones.length >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(mn) | O(m) | ## Note [reference](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/1049.%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8FII.md)