Skip to content

Baseball Game

EasyStack
Decode it first. Clues, constraints, pattern and plan before any code.

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.

["5", "2", "C", "D", "+"]                  →  30   (record: 5, 10, 15)
["5", "-2", "4", "C", "D", "9", "+", "+"]  →  27   (record: 5, -2, -4, 9, 5, 14)
["1", "C"]                                →  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.