Confusing LeetCode

Lawson Hung
2 min readOct 11, 2020

For the record, I was not able to solve this LeetCode problem.

682. Baseball Game

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:

  1. An integer x - Record a new score of x.
  2. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
  3. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
  4. "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.

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.

This is the code I came up with:

/**
* @param {string[]} ops
* @return {number}
*/
var calPoints = function(ops) {
let score = [];
for(let i = 0; i < ops.length; i++){
if(ops[i] === "+")
score.push(ops[i-2] + ops[i-1]);
else if (ops[i] === "D")
score.push(ops[i-1] * 2);
else if (ops[i] === "C")
ops.splice(i-1, 2);
else
score.push(ops[i]);
}

console.log(score);

return score;
};

The instructions itself were very confusing. I tried to solve it to the best of my ability though by seeing if this solution worked. I compared each item in ops to + D or C to see if it should be added, doubled or canceled. Then I push it into ops according to the rules of this very weird baseball game.

There were comments that this solution did not warrant the best examples, since there were errors in the examples. I hope I don’t run into more Leetcode problems like this!

--

--