Skip to content

visual walkthrough

Baseball Game

EasyStack SimulationReported at: Uber

Straightforward operation simulation to get comfortable with push and pop.

Solve on LeetCode

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

approachtimespace
Stack of scoresO(n)O(n)

More walkthroughs