Tap here to switch tabs
Problem
Submissions

Problem: Baseball Game

Baseball Game gives you operations that add scores, double the latest score, sum the previous two, or remove the latest. Apply them in order and return the total of the remaining scores.

Statement

You are keeping score for a baseball game with special rules. At the beginning, the record is empty.

You are given an array of strings operations, where operations[i] is the operation to apply at index i:

  • An integer x: Add a new score of xx to the record.

  • "+": Add a new score that is the sum of the previous 22 scores.

  • "D": Add a new score that is double the previous score.

  • "C": Invalidate and remove the previous score from the record.

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

Constraints:

  • 1≤1 \leq operations.length ≤1000\leq 1000

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

  • For operation "+", there will always be at least 22 previous scores on the record

  • For operations "C" and "D", there will always be at least 11 previous score on the record

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Baseball Game

Baseball Game gives you operations that add scores, double the latest score, sum the previous two, or remove the latest. Apply them in order and return the total of the remaining scores.

Statement

You are keeping score for a baseball game with special rules. At the beginning, the record is empty.

You are given an array of strings operations, where operations[i] is the operation to apply at index i:

  • An integer x: Add a new score of xx to the record.

  • "+": Add a new score that is the sum of the previous 22 scores.

  • "D": Add a new score that is double the previous score.

  • "C": Invalidate and remove the previous score from the record.

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

Constraints:

  • 1≤1 \leq operations.length ≤1000\leq 1000

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

  • For operation "+", there will always be at least 22 previous scores on the record

  • For operations "C" and "D", there will always be at least 11 previous score on the record