Skip to content
LC-1872 Hard LeetCode

1872. Stone Game VIII

Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 53% Topics: Array, Math, Dynamic Programming, Prefix Sum, Game Theory
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def stoneGameVIII(self, stones):
        """
        :type stones: List[int]
        :rtype: int
        """
        for i in xrange(len(stones)-1):
            stones[i+1] += stones[i]
        return reduce(lambda curr, i: max(curr, stones[i]-curr), reversed(xrange(1, len(stones)-1)), stones[-1])

Solution from kamyu104/LeetCode-Solutions · MIT