Easy Solution | 1ms - 100% beats | Fully explained | Java
Approach\n1. Initialize variables: sum to store the sum of scores, k as a pointer/index for the record array, and create an array record to store the scores.\n\
# Approach\n1. Initialize variables: `sum` to store the sum of scores, `k` as a pointer/index for the record array, and create an array `record` to store the scores.\n\n2. Iterate through each operation in the `operations` array using an enhanced for loop.\n\n3. For each operation:\n   - If the operation is "+", calculate the current score as the sum of the previous two scores in the record. Store the value in the `record` array at index `k` and increment `k` by 1.\n   - If the operation is "D", calculate the current score as double the previous score in the record. Store the value in the `record` array at index `k` and increment `k` by 1.\n   - If the operation is "C", invalidate the previous score by setting it to 0 in the `record` array. Decrement `k` by 1 to move the pointer/index back to the previous score.\n   - If the operation is a number, parse it as an integer and store it in the `record` array at index `k`. Increment `k` by 1.\n\n4. After processing all the operations, calculate the sum of scores by iterating through the `record` array using a regular for loop and adding each score to the `sum` variable.\n\n5. Return the final sum as the result.\n\nThe solution uses a switch-case statement to handle different types of operations efficiently. It updates the `record` array and maintains the pointer/index `k` to keep track of the latest position in the array. By the end of the loop, the `record` array contains the scores, and the sum is calculated, which is then returned as the final result.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution {\n    public int calPoints(String[] operations) {\n        int sum = 0, k = 0;\n        int[] record = new int[operations.length];\n        for(String s : operations)\n            switch(s){\n                case "+" -> {\n                    record[k] = record[k - 1] + record[k-2];\n                    k++;\n                } \n                case "D" -> {\n                    record[k] = record[k - 1] * 2;\n                    k++;\n                }\n                case "C" -> record[--k] = 0;\n                default -> {\n                    record[k] = Integer.parseInt(s);\n                    k++;\n                }\n            }\n\n        for(int num : record)\n            sum += num;\n\n        return sum;      \n    }\n}\n```
['Array', 'Stack', 'Simulation', 'Java']