Skip to content

Commit 41d76e6

Browse files
committed
add prob #682; O(N) in time and O(N) in space;
1 parent 2027716 commit 41d76e6

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

682_Baseball_Game.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
You're now a baseball game point recorder.
3+
4+
Given a list of strings, each string can be one of the 4 following types:
5+
6+
Integer (one round's score): Directly represents the number of points you get in this round.
7+
"+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
8+
"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.
9+
"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.
10+
Each round's operation is permanent and could have an impact on the round before and the round after.
11+
12+
You need to return the sum of the points you could get in all the rounds.
13+
14+
Example 1:
15+
Input: ["5","2","C","D","+"]
16+
Output: 30
17+
Explanation:
18+
Round 1: You could get 5 points. The sum is: 5.
19+
Round 2: You could get 2 points. The sum is: 7.
20+
Operation 1: The round 2's data was invalid. The sum is: 5.
21+
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
22+
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
23+
Example 2:
24+
Input: ["5","-2","4","C","D","9","+","+"]
25+
Output: 27
26+
Explanation:
27+
Round 1: You could get 5 points. The sum is: 5.
28+
Round 2: You could get -2 points. The sum is: 3.
29+
Round 3: You could get 4 points. The sum is: 7.
30+
Operation 1: The round 3's data is invalid. The sum is: 3.
31+
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
32+
Round 5: You could get 9 points. The sum is: 8.
33+
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
34+
Round 7: You could get 9 + 5 = 14 points. The sum is 27.
35+
Note:
36+
The size of the input list will be between 1 and 1000.
37+
Every integer represented in the list will be between -30000 and 30000.
38+
39+
来源:力扣(LeetCode)
40+
链接:https://leetcode-cn.com/problems/baseball-game
41+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
42+
*/
43+
44+
/* O(N) in time and O(N) in space */
45+
46+
#include <string>
47+
#include <vector>
48+
#include <stack>
49+
using namespace std;
50+
51+
class Solution {
52+
public:
53+
int calPoints(vector<string>& ops) {
54+
stack<int> s;
55+
int res = 0;
56+
57+
for(int i=0; i<(int)ops.size(); ++i){
58+
if(ops[i][0]=='+'){
59+
int prev1 = s.top(); s.pop();
60+
int prev2 = s.top(); s.pop();
61+
s.push(prev2);
62+
s.push(prev1);
63+
s.push(prev1+prev2);
64+
}
65+
else if(ops[i][0]=='D'){
66+
s.push(2*s.top());
67+
}
68+
else if(ops[i][0]=='C'){
69+
s.pop();
70+
}
71+
else{
72+
int temp = 0;
73+
bool is_pos = true;
74+
for(int j=0; j<(int)ops[i].size(); ++j){
75+
if(ops[i][j]=='-') is_pos = false;
76+
else temp = temp*10 + ops[i][j] - '0';
77+
}
78+
if(is_pos) s.push(temp);
79+
else s.push(-temp);
80+
}
81+
}
82+
83+
while(!s.empty()){
84+
res += s.top(); s.pop();
85+
}
86+
87+
return res;
88+
}
89+
};

0 commit comments

Comments
 (0)