aboutsummaryrefslogtreecommitdiffstats
path: root/1046/main.cpp
blob: 5789406e59c3edadd669e3e640bc489dcade363f (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
#include "header.hpp"
#include <algorithm>

class Solution {
public:
  static int lastStoneWeight(std::vector<int> &stones) {
    if (stones.size() == 1) {
      return stones[0];
    }
    std::sort(stones.begin(), stones.end());
    while (stones[stones.size() - 2] > 0) {
      stones[stones.size() - 2] =
          std::abs(stones[stones.size() - 1] - stones[stones.size() - 2]);
      stones[stones.size() - 1] = 0;
      std::sort(stones.begin(), stones.end());
      for (auto &iter : stones) {
        std::cout << iter << " " << std::endl;
      }
      std::cout << "\n";
    }
    return stones[stones.size() - 1];
  }
};

int main(int argc, char **argv) {
  std::vector<int> stones = {2, 7, 4, 1, 8, 1};
  std::cout << Solution::lastStoneWeight(stones);
  return 0;
}