forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0682-baseball-game.py
More file actions
24 lines (17 loc) · 792 Bytes
/
0682-baseball-game.py
File metadata and controls
24 lines (17 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def calPoints(self, operations: List[str]) -> int:
score_stack = []
for o in operations:
# it is +, D, or C
# if stack isn't of sufficient length, then operation is voided
if o == "+" and len(score_stack) >= 2:
summed = score_stack[-2] + score_stack[-1]
score_stack.append(summed)
elif o == "D" and len(score_stack) >= 1:
doubled = score_stack[-1] * 2
score_stack.append(doubled)
elif o == "C" and len(score_stack) >= 1:
score_stack.pop()
else:
score_stack.append(int(o))
return sum(score_stack)