682: Time 95.68%, Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nWe initialize an empty stack to keep track of the scores.\n\nWe iterate t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe initialize an empty stack to keep track of the scores.\n\nWe iterate through each operation in the input list.\n\nIf the operation is "C", we remove the last score from the stack using stack.pop().\n\nIf the operation is "D", we double the last score and append it to the stack using stack.append(stack[-1] * 2).\n\nIf the operation is "+", we add the last two scores and append the sum to the stack using stack.append(stack[-1] + stack[-2]).\n\nIf the operation is an integer, we convert it to an integer using int(op) and append it to the stack using stack.append(int(op)).\n\nFinally, we return the sum of all the scores in the stack using sum(stack).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def calPoints(self, ops: List[str]) -> int:\n        stack = []\n        for op in ops:\n            if op == "C":\n                stack.pop()\n            elif op == "D":\n                stack.append(stack[-1] * 2)\n            elif op == "+":\n                stack.append(stack[-1] + stack[-2])\n            else:\n                stack.append(int(op))\n        return sum(stack)\n\n```
['Array', 'Stack', 'Simulation', 'Python', 'Python3']