-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0682-baseball-game.js
More file actions
41 lines (37 loc) · 1.12 KB
/
0682-baseball-game.js
File metadata and controls
41 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Baseball Game
* Time Complexity: O(N)
* Space Complexity: O(N)
*/
var calPoints = function (operationsList) {
const scoreRecord = [];
for (
let currentOperationIndex = 0;
currentOperationIndex < operationsList.length;
currentOperationIndex++
) {
const currentOperation = operationsList[currentOperationIndex];
if (currentOperation === "C") {
scoreRecord.pop();
} else if (currentOperation === "D") {
const previousScore = scoreRecord[scoreRecord.length - 1];
scoreRecord.push(previousScore * 2);
} else if (currentOperation === "+") {
const firstPreviousScore = scoreRecord[scoreRecord.length - 1];
const secondPreviousScore = scoreRecord[scoreRecord.length - 2];
scoreRecord.push(firstPreviousScore + secondPreviousScore);
} else {
scoreRecord.push(parseInt(currentOperation));
}
}
let totalScore = 0;
for (
let currentScoreIndex = 0;
currentScoreIndex < scoreRecord.length;
currentScoreIndex++
) {
const currentScoreValue = scoreRecord[currentScoreIndex];
totalScore += currentScoreValue;
}
return totalScore;
};