Skip to content

Commit 75f9f68

Browse files
authored
Create baseball-game.cpp
1 parent 7bd7d81 commit 75f9f68

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

C++/baseball-game.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int calPoints(vector<string>& ops) {
7+
vector<int> records;
8+
for (const auto& op : ops) {
9+
if (op == "+") {
10+
records.emplace_back(records[records.size() - 2] + records.back());
11+
} else if (op == "D") {
12+
records.emplace_back(2 * records.back());
13+
} else if (op == "C") {
14+
records.pop_back();
15+
} else {
16+
records.emplace_back(stoi(op));
17+
}
18+
}
19+
return accumulate(records.begin(), records.end(), 0);
20+
}
21+
};

0 commit comments

Comments
 (0)