-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path682.Baseball_Game.js
More file actions
187 lines (155 loc) · 4.77 KB
/
682.Baseball_Game.js
File metadata and controls
187 lines (155 loc) · 4.77 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* 682. Baseball Game
*
* You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:
An integer x.
Record a new score of x.
'+'.
Record a new score that is the sum of the previous two scores.
'D'.
Record a new score that is the double of the previous score.
'C'.
Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.
The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.
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","C"]
Output: 0
Explanation:
"1" - Add 1 to the record, record is now [1].
"C" - Invalidate and remove the previous score, record is now [].
Since the record is empty, the total sum is 0.
Constraints:
1 <= operations.length <= 1000
operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
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.
*/
var utilities = {
"+": function (stack) {
const last = stack[stack.length - 1] ?? 0;
const secondLast = stack[stack.length - 2] ?? 0;
stack.push(last + secondLast);
},
C: function (stack) {
stack.pop();
},
D: function (stack) {
const last = stack[stack.length - 1] ?? 0;
stack.push(last * 2);
},
};
// NAIVE APPROACH
function findFinalScore(ops) {
const stack = [];
const sum = 0;
for (let i = 0; i < ops.length; i++) {
if (!isNaN(ops[i])) {
stack.push(Number(ops[i]));
} else {
utilities[ops[i]](stack);
}
}
return stack.reduce((accumulator, current) => {
return accumulator + current;
}, 0);
}
var ops = ["5", "-2", "4", "C", "D", "9", "+", "+"];
console.log(findFinalScore(ops));
// Better Approach
// BETTER APPROACH
var utilities = {
sum: 0,
"+": function (stack) {
const last = stack[stack.length - 1] ?? 0;
const secondLast = stack[stack.length - 2] ?? 0;
stack.push(last + secondLast);
this.sum += last + secondLast;
},
C: function (stack) {
const last = stack.pop();
this.sum -= last;
},
D: function (stack) {
const last = stack[stack.length - 1] ?? 0;
stack.push(last * 2);
this.sum += last * 2;
},
};
function findFinalScore(ops) {
const stack = [];
for (let i = 0; i < ops.length; i++) {
if (!isNaN(ops[i])) {
stack.push(Number(ops[i]));
utilities.sum = utilities.sum + Number(ops[i]);
} else {
utilities[ops[i]](stack);
}
}
return utilities.sum;
}
var ops = ["5", "-2", "4", "C", "D", "9", "+", "+"];
var ops = ["1", "C"];
console.log(findFinalScore(ops));
// ANOTHER SOLUTION
function findFinalScore(ops) {
const stack = [];
var sum = 0;
for (let i = 0; i < ops.length; i++) {
switch (ops[i]) {
case "+":
{
const last = stack[stack.length - 1] ?? 0;
const secondLast = stack[stack.length - 2] ?? 0;
stack.push(last + secondLast);
sum = sum + last + secondLast;
}
break;
case "C":
{
const last = stack.pop();
sum -= last;
}
break;
case "D":
{
const last = stack[stack.length - 1] ?? 0;
stack.push(last * 2);
sum += last * 2;
}
break;
default:
stack.push(Number(ops[i]));
sum = sum + Number(ops[i]);
break;
}
}
return sum;
}
var ops = ["5", "-2", "4", "C", "D", "9", "+", "+"];
var ops = ["1", "C"];
console.log(findFinalScore(ops));