Stack pattern
Baseball Game
You are given a list of operations: an integer records that score, "+" records the sum of the previous two, "D" records double the previous one, and "C" cancels the previous one. Return the sum of all scores at the end. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 682
- Easy
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
StackLeetCode 682
Easy
From the first idea to the best one
- 01 Brute force · list with index maths O(n)O(n) space
- 02 Optimized · stack O(n)O(n) space
What you will learn
- How to read Baseball Game and spot the stack pattern in it
- Brute force · list with index maths: O(n) time, O(n) space
- Optimized · stack: O(n) time, O(n) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Stack
See Baseball Game run, step by step
Sign in and this page opens on the animation, with all 8 Stack problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.