Skip to content
LC-0682 Easy LeetCode

682. Baseball Game

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 79% Topics: Array, Stack, Simulation
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

class Solution(object):
    def calPoints(self, ops):
        """
        :type ops: List[str]
        :rtype: int
        """
        history = []
        for op in ops:
            if op == '+':
                history.append(history[-1] + history[-2])
            elif op == 'D':
                history.append(history[-1] * 2)
            elif op == 'C':
                history.pop()
            else:
                history.append(int(op))
        return sum(history)

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions