Skip to content

Commit 40e19be

Browse files
682. Baseball Game
Difficulty: Easy 39 / 39 test cases passed. Runtime: 36 ms Memory Usage: 14.1 MB
1 parent 650bce5 commit 40e19be

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Easy/682.BaseballGame.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
You are keeping score for a baseball game with strange rules. The game
3+
consists of several rounds, where the scores of past rounds may affect
4+
future rounds' scores.
5+
6+
At the beginning of the game, you start with an empty record. You are given
7+
a list of strings ops, where ops[i] is the ith operation you must apply to
8+
the record and is one of the following:
9+
1. An integer x - Record a new score of x.
10+
2. "+" - Record a new score that is the sum of the previous two scores.
11+
It is guaranteed there will always be two previous scores.
12+
3. "D" - Record a new score that is double the previous score. It is
13+
guaranteed there will always be a previous score.
14+
4. "C" - Invalidate the previous score, removing it from the record.
15+
It is guaranteed there will always be a previous score.
16+
17+
Return the sum of all the scores on the record.
18+
19+
Example:
20+
Input: ops = ["5","2","C","D","+"]
21+
Output: 30
22+
Explanation:
23+
"5" - Add 5 to the record, record is now [5].
24+
"2" - Add 2 to the record, record is now [5, 2].
25+
"C" - Invalidate and remove the previous score, record is now
26+
[5].
27+
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
28+
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
29+
The total sum is 5 + 10 + 15 = 30.
30+
31+
Example:
32+
Input: ops = ["5","-2","4","C","D","9","+","+"]
33+
Output: 27
34+
Explanation:
35+
"5" - Add 5 to the record, record is now [5].
36+
"-2" - Add -2 to the record, record is now [5, -2].
37+
"4" - Add 4 to the record, record is now [5, -2, 4].
38+
"C" - Invalidate and remove the previous score, record is now
39+
[5, -2].
40+
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
41+
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
42+
"+" - Add -4 + 9 = 5 to the record, record is now
43+
[5, -2, -4, 9, 5].
44+
"+" - Add 9 + 5 = 14 to the record, record is now
45+
[5, -2, -4, 9, 5, 14].
46+
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
47+
48+
Example:
49+
Input: ops = ["1"]
50+
Output: 1
51+
52+
Constraints:
53+
- 1 <= ops.length <= 1000
54+
- ops[i] is "C", "D", "+", or a string representing an integer in the
55+
range [-3 * 10**4, 3 * 10**4].
56+
- For operation "+", there will always be at least two previous scores
57+
on the record.
58+
- For operations "C" and "D", there will always be at least one
59+
previous score on the record.
60+
"""
61+
#Difficulty: Easy
62+
#39 / 39 test cases passed.
63+
#Runtime: 36 ms
64+
#Memory Usage: 14.1 MB
65+
66+
#Runtime: 36 ms, faster than 85.56% of Python3 online submissions for Baseball Game.
67+
#Memory Usage: 14.1 MB, less than 100.00% of Python3 online submissions for Baseball Game.
68+
69+
class Solution:
70+
def calPoints(self, ops: List[str]) -> int:
71+
scores = []
72+
for n in ops:
73+
if n == "+":
74+
scores.append(scores[-1] + scores[-2])
75+
elif n == "D":
76+
scores.append(scores[-1] * 2)
77+
elif n == "C":
78+
scores.pop()
79+
else:
80+
scores.append(int(n))
81+
return sum(scores)

0 commit comments

Comments
 (0)