visual walkthrough
Baseball Game
Straightforward operation simulation to get comfortable with push and pop.
The idea
Every rule refers to the most recent valid scores, and C removes the latest one. That is exactly a stack: push new scores, pop to cancel, peek at the top (or top two) for D and +.
The final score is the sum of whatever remains.
Complexity
| approach | time | space |
|---|---|---|
| Stack of scores | O(n) | O(n) |