Confusing LeetCode

  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.
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.
/**
* @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;
};

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Action required] Your RSS.app Trial has Expired — Fri Dec 31 2021

Part 1: Fabric.js on React - fabric.Canvas(‘…’)

How to: Lab Scene, Turn people into zombies — The Sandbox Game Maker v0.37

Add Meta Tags in Client-side React App

Chuseok and Udacity

11 Micro Frontends Frameworks You Should Know

Implementing Binary Tree & Linked Lists in TypeScript is a must-have in your programming arsenal…

Weekly update on development process (Dec 20, 2021)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lawson Hung

Lawson Hung

Software Engineer

More from Medium

LeetCode — Repeated Substring Pattern

Leetcode — Total number of substring palindromes

39. Combination Sum

Understand Recursion