aboutsummaryrefslogtreecommitdiffstats
path: root/682/main.cpp
blob: 25432b84e244a1cad55d42604a6546d07e4fbcfc (plain) (blame)
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
35
36
37
38
39
40
#include "header.hpp"
#include <string>

class Solution {
public:
  static int calPoints(std::vector<std::string> &ops) {
    std::vector<int> result;
    result.reserve(1000);
    int sum = 0;
    for (auto &iter : ops) {
      if (iter == "+") {
        auto dummy = result[result.size() - 1] + result[result.size() - 2];
        result.push_back(dummy);
        sum += dummy;
      } else if (iter == "D") {
        auto dummy = result[result.size() - 1] * 2;
        result.push_back(dummy);
        sum += dummy;
      } else if (iter == "C") {
        sum -= result[result.size() - 1];
        result.pop_back();
      } else {
        auto dummy = std::stoi(iter);
        result.push_back(dummy);
        sum += dummy;
      }
    }

    return sum;
  }
};

int main(int argc, char **argv) {
  std::vector<std::string> ops1 = {"5", "2", "C", "D", "+"};
  std::vector<std::string> ops2 = {"5", "-2", "4", "C", "D", "9", "+", "+"};
  std::cout << Solution::calPoints(ops1) << "\n";
  std::cout << Solution::calPoints(ops2) << "\n";
  return 0;
}