Baseball Game

You keep the score sheet for a baseball game with unusual rules. You are given a list of strings `operations`, applied in order to an initially empty record. An integer `x` records a new score of `x`. `"+"` records a new score equal to the sum of the previous two scores. `"D"` records a new score equal to double the previous score. `"C"` removes the previous score from the record. Return the sum of all scores left on the record after every operation. All operations are valid, and every intermediate value and the answer fit in a 32-bit integer.

30:00

Baseball Game
easy
Free written solution
Topics
Companies

You keep the score sheet for a baseball game with unusual rules. You are given a list of strings operations, applied in order to an initially empty record. An integer x records a new score of x. "+" records a new score equal to the sum of the previous two scores. "D" records a new score equal to double the previous score. "C" removes the previous score from the record. Return the sum of all scores left on the record after every operation. All operations are valid, and every intermediate value and the answer fit in a 32-bit integer.

Example 1:
Input: {"operations":["5","2","C","D","+"]}
Output: 30
Constraints:
  • 1≤operations.length≤10001 \leq \text{operations.length} \leq 1000

  • operations[i] is "C", "D", "+", or a string for an integer in the range [−3×104,3×104][-3 \times 10^4, 3 \times 10^4].

  • For "+", there are always at least two previous scores on the record.

  • For "C" and "D", there is always at least one previous score on the record.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"operations":["5","2","C","D","+"]}

operations

5
2
C
D
+

Start with an empty score sheet. Every operation only looks at the most recent scores, so the sheet is a stack.

score sheet (top of stack first)

0 kept

empty sheet

this step

ready

sum of the sheet

0

+ sum of top twoD double topC erase top
Variables
VariableValue
record[]
sum0
DepthFunction Call
Stack empty
0/7