Skip to content
Decoder

Baseball Game

Easy

Problem

You keep score for a game with odd rules. You get a list operations, applied in order to a record that starts empty. Each operation is one of:

- An integer x: record a new score of x. - "+": record a new score equal to the sum of the previous two scores. - "D": record a new score equal to double the previous score. - "C": cancel the previous score, removing it from the record.

Return the sum of all scores left in the record.

Example 1

Input
["5", "2", "C", "D", "+"]
Output
30
Why
record: 5, 10, 15

Example 2

Input
["5", "-2", "4", "C", "D", "9", "+", "+"]
Output
27
Why
record: 5, -2, -4, 9, 5, 14

Example 3

Input
["1", "C"]
Output
0

Constraints

  • 1 <= operations.length <= 1000
  • Each operation is "C", "D", "+", or an integer in [-3 * 10^4, 3 * 10^4].
  • Every "+" has at least two scores before it, and every "C" and "D" at least one.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.