Categories:

Tags:



Problem

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 ith 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 the double of 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.

Constraints

  • 1 <= operations.length <= 1000
  • operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
  • 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.

Solution

The problem Baseball Game can be solved by storing previous scores on a stack and performing operations based on the instructions given.

Implementation

class Solution
{
  public:
    int calPoints(vector<string> &operations)
    {
        vector<int> score;
        for (string operation : operations)
        {
            if (operation == "+")
                score.push_back(score.back() + score[score.size() - 2]);
            else if (operation == "D")
                score.push_back(score.back() * 2);
            else if (operation == "C")
                score.pop_back();
            else
                score.push_back(stoi(operation));
        }

        return accumulate(score.begin(), score.end(), 0);
    }
};