Skip to content
LC-1049 Medium LeetCode

1049. Last Stone Weight II

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 57% Topics: Array, Dynamic Programming
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(2^n)
# Space: O(2^n)

class Solution(object):
    def lastStoneWeightII(self, stones):
        """
        :type stones: List[int]
        :rtype: int
        """
        dp = {0}
        for stone in stones:
            dp |= {stone+i for i in dp}
        S = sum(stones)
        return min(abs(i-(S-i)) for i in dp)

Solution from kamyu104/LeetCode-Solutions · MIT