JavaScript Easiest way, Beats 96%.
Intuition\n Describe your first thoughts on how to solve this problem. \nUse a stack to keep track of valid scores and perform operations based on the given rul
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse a stack to keep track of valid scores and perform operations based on the given rules. Calculate the total score by summing up the scores in the stack.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitialize an empty stack.\nIterate through operations.\nFor each operation:\nIf integer, push onto stack.\nIf "+", push sum of last two scores onto stack.\nIf "D", push double of last score onto stack.\nIf "C", pop last score from stack.\nReturn sum of scores in stack.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n), where n is the number of operations.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(n), where n is the number of operations. The stack stores the valid scores, which can be at most the same size as the number of operations.\n# Code\n```\n/**\n * @param {string[]} operations\n * @return {number}\n */\nvar calPoints = function(operations) {\n    let stack = [];\n    for(let i =0; i< operations.length; i++){\n        let len = stack.length;\n        switch(operations[i]){\n            case "+" : \n                    stack.push( stack[len-1]+stack[len-2] );\n                    break;\n            case "D" : \n                    stack.push(stack[len-1]*2);\n                    break;\n            case "C" : \n                    stack.pop()\n                    break;\n            default : \n                    stack.push(parseInt(operations[i]))\n        }\n    }\n    \n    return stack.length ? stack.reduce((now,prev)=>{\n        return now+prev\n    }) : 0;\n};\n```\n# EXPLANATION\nLet\'s go through an example to understand how the code works:\n```\nInput: operations = ["5", "2", "C", "D", "+"]\nOutput: 30\n\n```\n\nInitially, the stack is empty. We iterate through the operations:\n\nFor "5", we convert it to a number and push it onto the stack. The stack becomes [5].\nFor "2", we convert it to a number and push it onto the stack. The stack becomes [5, 2].\nFor "C", we pop the last score from the stack. The stack becomes [5].\nFor "D", we duplicate the last score in the stack (5) and push the doubled value (10) onto the stack. The stack becomes [5, 10].\nFor "+", we pop the last two scores from the stack (5 and 10), calculate their sum (15), and push it onto the stack. The stack becomes [5, 10, 15].\nWe have processed all the operations. We calculate the\n\n\n\n\n
['Array', 'Stack', 'JavaScript']