Skip to content

Commit 0024c83

Browse files
committed
update: solution for baseball game
1 parent e79c74f commit 0024c83

File tree

1 file changed

+101
-0
lines changed
  • src/_DataStructures_/Stack/baseball-game

1 file changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
3+
You're now a baseball game point recorder.
4+
5+
Given a list of strings, each string can be one of the 4 following types:
6+
7+
Integer (one round's score): Directly represents the number of points you get in this round.
8+
"+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
9+
"D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
10+
"C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.
11+
Each round's operation is permanent and could have an impact on the round before and the round after.
12+
13+
You need to return the sum of the points you could get in all the rounds.
14+
15+
*/
16+
17+
const Stack = require('../index');
18+
19+
// The given solution is on O(n)
20+
21+
function sumOfPoints(arr) {
22+
const pointsTracker = new Stack();
23+
let sum = 0;
24+
25+
if (!Array.isArray(arr)) {
26+
throw new Error('Invalid Argument!');
27+
}
28+
// Track the value of `sum` accordingly
29+
for (let el of arr) {
30+
// console.log('data : ', pointsTracker.data);
31+
// console.log('sum : ', sum);
32+
33+
if (el === 'C') {
34+
// pop if the value is `C`
35+
const top = pointsTracker.pop();
36+
if (top) {
37+
sum -= top;
38+
}
39+
} else if (el === 'D') {
40+
// Double the top id the value is 'D'
41+
const top = pointsTracker.peek();
42+
sum += top * 2;
43+
pointsTracker.push(top * 2);
44+
} else if (el === '+') {
45+
const top = pointsTracker.pop();
46+
const secondTop = pointsTracker.peek();
47+
48+
const result = top + secondTop;
49+
sum += result;
50+
51+
pointsTracker.push(top);
52+
pointsTracker.push(result);
53+
} else {
54+
// push to the Stack if the value is integer
55+
sum += parseInt(el);
56+
pointsTracker.push(parseInt(el));
57+
}
58+
}
59+
return sum;
60+
}
61+
62+
/**
63+
* Let's Try it out
64+
65+
console.log(sumOfPoints(['5', '2', 'C', 'D', '+']));
66+
console.log(sumOfPoints(['5', '-2', '4', 'C', 'D', '9', '+', '+']));
67+
*/
68+
69+
/**
70+
71+
Input: ["5","2","C","D","+"]
72+
Output: 30
73+
74+
Explanation:
75+
76+
Round 1: You could get 5 points. The sum is: 5.
77+
Round 2: You could get 2 points. The sum is: 7.
78+
Operation 1: The round 2's data was invalid. The sum is: 5.
79+
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
80+
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
81+
Example 2:
82+
Input: ["5","-2","4","C","D","9","+","+"]
83+
Output: 27
84+
85+
Explanation:
86+
87+
Round 1: You could get 5 points. The sum is: 5.
88+
Round 2: You could get -2 points. The sum is: 3.
89+
Round 3: You could get 4 points. The sum is: 7.
90+
Operation 1: The round 3's data is invalid. The sum is: 3.
91+
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
92+
Round 5: You could get 9 points. The sum is: 8.
93+
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
94+
Round 7: You could get 9 + 5 = 14 points. The sum is 27.
95+
96+
Note:
97+
98+
The size of the input list will be between 1 and 1000.
99+
Every integer represented in the list will be between -30000 and 30000.
100+
101+
*/

0 commit comments

Comments
 (0)