Baseball Game
You are keeping score for a game. You get a list of operations, and you must return the sum of all scores on the record at the end. An operation is one of four things: a number records that score. C cancels the previous score and removes it. D records double the previous score. + records the sum of the previous two scores. A stack fits perfectly, because every operation only ever looks at or removes the most recent scores — exactly what the top of a stack gives you.
Constraints
- 1 ≤ operations.length ≤ 1000
- Each entry is an integer written as text, or 'C', 'D' or '+'
- 'C' only appears when there is at least one score on the record
- 'D' only appears when there is at least one score, and '+' only when there are at least two
- The final total fits in a normal integer
Example
operations = ["5", "2", "C", "D", "+"]30Explanation Record 5, record 2, cancel the 2, double the 5 to get 10, then add 5 + 10 to get 15. The record holds 5, 10 and 15, which sum to 30.
In plain terms
- Stack
- A pile where you add and remove only at the top, so the most recent item is always the easiest one to reach.
- Record
- The running list of scores. Here it is the stack, bottom to top in the order they happened.
- Cancel
- Removing the most recent score from the record, as if it never happened.
The row is the score record, oldest on the left, newest on the right
The record starts empty, before any operation is read.
What happens in this step
operations = ["5", "2", "C", "D", "+"] stack = [] Nothing is on the record yet, so every slot shows a dash.
Steps to visualize
- Each filled cell is one score on the record. A dash means an empty slot.
- The frame marks the cell the current operation is touching.
- A number is pushed straight onto the top.
- C pops the top cell off. D pushes double the top. + pushes the sum of the top two.
- At the end, add up every filled cell to get the answer.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
The record starts empty, before any operation is read.
What happens in this step
operations = ["5", "2", "C", "D", "+"] stack = [] Nothing is on the record yet, so every slot shows a dash.
Solution
function calPoints(operations) {
const stack = [];
for (const op of operations) {
if (op === 'C') {
stack.pop();
} else if (op === 'D') {
stack.push(stack[stack.length - 1] * 2);
} else if (op === '+') {
stack.push(stack[stack.length - 1] + stack[stack.length - 2]);
} else {
stack.push(Number(op));
}
}
let total = 0;
for (const score of stack) {
total += score;
}
return total;
}- Time
- O(n)
- Space
- O(n)
Test cases
| Input | Expected | Covers |
|---|---|---|
operations = ["5", "2", "C", "D", "+"] | 30 | example from the description |
operations = ["7"] | 7 | smallest input, one score and nothing else |
operations = ["3", "C"] | 0 | the only score is cancelled, leaving an empty record |
operations = ["-2", "5", "+", "D"] | 12 | negative scores mixed with + and D |
operations = ["5", "-2", "4", "C", "D", "9", "+", "+"] | 27 | a longer list using every operation |
operations = ["1", "D", "D", "D"] | 15 | doubling over and over |