Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3fafb8

Browse files
committed
O(n) time and O(n) space using stack
1 parent 4f541e8 commit c3fafb8

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
3+
4+
At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:
5+
6+
An integer x - Record a new score of x.
7+
"+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
8+
"D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
9+
"C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
10+
Return the sum of all the scores on the record.
11+
12+
13+
14+
Example 1:
15+
16+
Input: ops = ["5","2","C","D","+"]
17+
Output: 30
18+
Explanation:
19+
"5" - Add 5 to the record, record is now [5].
20+
"2" - Add 2 to the record, record is now [5, 2].
21+
"C" - Invalidate and remove the previous score, record is now [5].
22+
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
23+
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
24+
The total sum is 5 + 10 + 15 = 30.
25+
Example 2:
26+
27+
Input: ops = ["5","-2","4","C","D","9","+","+"]
28+
Output: 27
29+
Explanation:
30+
"5" - Add 5 to the record, record is now [5].
31+
"-2" - Add -2 to the record, record is now [5, -2].
32+
"4" - Add 4 to the record, record is now [5, -2, 4].
33+
"C" - Invalidate and remove the previous score, record is now [5, -2].
34+
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
35+
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
36+
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
37+
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
38+
The total sum is 5 +たす -ひく2 +たす -ひく4 +たす 9 +たす 5 +たす 14 = 27.
39+
Example 3:
40+
41+
Input: ops = ["1"]
42+
Output: 1
43+
44+
45+
Constraints:
46+
47+
1 <= ops.length <= 1000
48+
ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
49+
For operation "+", there will always be at least two previous scores on the record.
50+
For operations "C" and "D", there will always be at least one previous score on the record.
51+
"""
52+
class Solution:
53+
def calPoints(self, ops: List[str]) -> int:
54+
result = []
55+
for op in ops:
56+
if op.lstrip("-").isdigit():
57+
result.append(int(op))
58+
elif op == "+":
59+
result.append(result[-1]+result[-2])
60+
elif op == "D":
61+
result.append(2*result[-1])
62+
elif op == "C":
63+
result.pop()
64+
return sum(result)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /