682. Baseball Game
Problem Description
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:
- An integer
x- Record a new score ofx. "+"- Record a new score that is the sum of the previous two scores. It is guaranteed there will always be at least two previous scores."D"- Record a new score that is double the previous score. It is guaranteed there will always be a previous score."C"- Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
Return the sum of all the scores on the record.
Examples:
Example 1:
Input: ops = ["5","2","C","D","+"] Output: 30 Explanation: "5" - Add 5 to the record, record is now [5]. "2" - Add 2 to the record, record is now [5, 2]. "C" - Invalidate and remove the previous score, record is now [5]. "D" - Add 2 * 5 = 10 to the record, record is now [5, 10]. "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15]. The total sum is 5 + 10 + 15 = 30.
Example 2:
Input: ops = ["5","-2","4","C","D","9","+","+"] Output: 27 Explanation: "5" - Add 5 to the record, record is now [5]. "-2" - Add -2 to the record, record is now [5, -2]. "4" - Add 4 to the record, record is now [5, -2, 4]. "C" - Invalidate and remove the previous score, record is now [5, -2]. "D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4]. "9" - Add 9 to the record, record is now [5, -2, -4, 9]. "+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5]. "+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14]. The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Example 3:
Input: ops = ["1"] Output: 1
Constraints:
- 1 ≤ ops.length ≤ 1000
- ops[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
Python Solution
class Solution:
def calPoints(self, ops: List[str]) -> int:
stack = []
for op in ops:
if op == '+':
stack.append(stack[-1] + stack[-2])
elif op == 'D':
stack.append(2 * stack[-1])
elif op == 'C':
stack.pop()
else:
stack.append(int(op))
return sum(stack)
Implementation Notes:
- Uses stack to keep track of scores
- Time complexity: O(n)
- Space complexity: O(n)
- Handles all operations efficiently
Java Solution
class Solution {
public int calPoints(String[] ops) {
Stack stack = new Stack<>();
for (String op : ops) {
if (op.equals("+")) {
int top = stack.pop();
int newTop = top + stack.peek();
stack.push(top);
stack.push(newTop);
} else if (op.equals("D")) {
stack.push(2 * stack.peek());
} else if (op.equals("C")) {
stack.pop();
} else {
stack.push(Integer.parseInt(op));
}
}
int sum = 0;
for (int score : stack) {
sum += score;
}
return sum;
}
}
C++ Solution
class Solution {
public:
int calPoints(vector& ops) {
vector stack;
for (const string& op : ops) {
if (op == "+") {
stack.push_back(stack[stack.size()-1] + stack[stack.size()-2]);
} else if (op == "D") {
stack.push_back(2 * stack.back());
} else if (op == "C") {
stack.pop_back();
} else {
stack.push_back(stoi(op));
}
}
return accumulate(stack.begin(), stack.end(), 0);
}
};
JavaScript Solution
/**
* @param {string[]} ops
* @return {number}
*/
var calPoints = function(ops) {
const stack = [];
for (const op of ops) {
if (op === '+') {
stack.push(stack[stack.length-1] + stack[stack.length-2]);
} else if (op === 'D') {
stack.push(2 * stack[stack.length-1]);
} else if (op === 'C') {
stack.pop();
} else {
stack.push(parseInt(op));
}
}
return stack.reduce((sum, score) => sum + score, 0);
};
C# Solution
public class Solution {
public int CalPoints(string[] ops) {
var stack = new List();
foreach (string op in ops) {
if (op == "+") {
stack.Add(stack[stack.Count-1] + stack[stack.Count-2]);
} else if (op == "D") {
stack.Add(2 * stack[stack.Count-1]);
} else if (op == "C") {
stack.RemoveAt(stack.Count-1);
} else {
stack.Add(int.Parse(op));
}
}
return stack.Sum();
}
}
Implementation Notes:
- Uses stack/list to keep track of scores
- Time complexity: O(n)
- Space complexity: O(n)
- Handles all operations efficiently