Baseball Game

You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.

You are given a list of strings operations, where operations[i] is the i^th operation you must apply to the record and is one of the following:

  • An integer x: record a new score of x.
  • "+": record a new score that is the sum of the previous two scores.
  • "D": record a new score that is double the previous score.
  • "C": invalidate the previous score, removing it from the record.

Return the sum of all the scores on the record after applying all the operations.

The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.

Example 1
Inputoperations = ["5","2","C","D","+"]
Output30
After applying the operations, the record is [5, 10, 15], whose total sum is 30.
Example 2
Inputoperations = ["5","-2","4","C","D","9","+","+"]
Output27
After applying the operations, the record is [5, -2, -4, 9, 5, 14], whose total sum is 27.

Constraints

  • 1 <= operations.length <= 1000
  • operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10^4, 3 * 10^4].
  • For operation "+", there will always be at least two previous scores on the record.
  • For operations "C" and "D", there will always be at least one previous score on the record.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate