-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathBaseball Game
More file actions
34 lines (33 loc) · 954 Bytes
/
Baseball Game
File metadata and controls
34 lines (33 loc) · 954 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
25
26
27
28
29
30
31
32
33
34
/*
Company Tags : Amazon (1st Round, 2020 Hackerrank Online Test)
NOTE: The question name was different. It was "Robot Game" (They may change the name)
Leetcode Link : https://leetcode.com/problems/baseball-game/
*/
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> st;
for(string s:ops) {
if(s != "C" && s != "D" && s != "+") {
st.push(stoi(s));
} else if(s == "+") {
int temp1 = st.top();
st.pop();
int temp2 = st.top();
st.push(temp1);
st.push(temp1+temp2);
} else if(s == "D") {
int temp = st.top();
st.push(2*temp);
} else {
st.pop();
}
}
int sum = 0;
while(!st.empty()) {
sum += st.top();
st.pop();
}
return sum;
}
};