Scorekeeper With Undo
A scorekeeper records points as a list of operations ops:
- an integer string such as "5" or "-2" records that many points;
- "x2" records double the most recent recorded score;
- "sum" records the sum of the two most recent recorded scores;
- "undo" removes the most recent recorded score.
Every operation is valid when applied. Return the total of all scores still recorded at the end.
Example 1
Input: ops = ["5", "2", "undo", "x2", "sum"] Output: 30 Explanation: Records become [5], [5, 2], [5], [5, 10], [5, 10, 15].
Example 2
Input: ops = ["1", "undo"] Output: 0
Constraints
- 0 <= ops.length <= 10^5
- Recorded scores stay within ±10^9.
Hint 1
Keep the recorded scores on a stack.
Hint 2
Every operation only looks at the top one or two entries.
Discussion 0
Join free to post in the discussion.
Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Game Developer
Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.