⚡Easy Solution ✅ || STACK 📚 || Explanation 💡|| Python3🐍 || Simulation🎮 || Array⚡
Problem\nGiven a list of strings representing operations in a game, where each operation can be one of the following:\n1. "C": Cancel the last valid round\'s sc
# Problem\nGiven a list of strings representing operations in a game, where each operation can be one of the following:\n1. "C": Cancel the last valid round\'s score.\n2. "D": Double the last valid round\'s score.\n3. "+": Add the last two valid round\'s scores.\n4. A number: Record a new score.\n\nCalculate the total sum of all the valid scores based on the given operations.\n\n---\n\n# Approach\n1. Initialize an empty stack to store valid scores and a variable to store the total sum.\n2. Iterate through each operation in the list.\n3. For each operation:\n   - If the operation is "C", cancel the last valid round\'s score by subtracting it from the total sum.\n   - If the operation is "D", double the last valid round\'s score, add it to the stack, and update the total sum.\n   - If the operation is "+", add the last two valid round\'s scores, add the sum to the stack, and update the total sum.\n   - If the operation is a number, record a new score by converting it to an integer, add it to the stack, and update the total sum.\n4. Return the total sum as the result.\n\n---\n\n# Explanation\nThe function `calPoints` takes a list of strings `operations` representing game operations.\nIt iterates through each operation and calculates the total sum of valid scores based on the operations.\nThe total sum is calculated by applying the rules defined for each operation: cancel, double, add, or record a new score.\nThe implementation uses a stack to keep track of valid scores and updates the total sum accordingly.\nFinally, it returns the total sum as the result.\n\n---\n\n\n# Code\n```\nclass Solution:\n    def calPoints(self, operations: List[str]) -> int:\n        # Initialize an empty stack to store valid scores\n        stack = []\n        \n        # Initialize a variable to store the total sum\n        total_sum = 0\n        \n        # Iterate through each operation in the list\n        for op in operations:\n            if op == \'C\':  # If the operation is \'C\' (Cancel the last valid round\'s score)\n                # Subtract the last valid round\'s score from the total sum\n                total_sum -= stack.pop()\n                \n            elif op == \'D\':  # If the operation is \'D\' (Double the last valid round\'s score)\n                # Double the last valid round\'s score and add it to the stack and total sum\n                score = stack[-1] * 2\n                stack.append(score)\n                total_sum += score\n                \n            elif op == \'+\':  # If the operation is \'+\' (Add the last two valid round\'s scores)\n                # Calculate the sum of the last two valid round\'s scores\n                score = stack[-1] + stack[-2]\n                stack.append(score)\n                total_sum += score\n                \n            else:  # If the operation is a number (Record a new score)\n                # Convert the operation to an integer and add it to the stack and total sum\n                score = int(op)\n                stack.append(score)\n                total_sum += score\n                \n        # Return the total sum\n        return total_sum\n\n```\n# *Did you upvote?? \u2764\uFE0F*\n![image.png](https://assets.leetcode.com/users/images/af09321d-1586-4e01-8b5a-dcd2d1768785_1712561123.2021847.png)\n\n
['Array', 'Stack', 'Simulation', 'Python3']