aboutsummaryrefslogtreecommitdiff
path: root/last_stone_weight.cpp
blob: c7422db4678945659e7117109053ae2475de60f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "leetcode.h"

class Solution {
public:
  int lastStoneWeight(vector<int> &stones) {
    make_heap(stones.begin(), stones.end());
    while (stones.size() > 1) {
      int x = stones.front();
      pop_heap(stones.begin(), stones.end());
      stones.pop_back();
      int y = stones.front();
      pop_heap(stones.begin(), stones.end());
      stones.pop_back();
      if (x == y)
        continue;
      stones.push_back(x - y);
      push_heap(stones.begin(), stones.end());
    }
    return stones.size() ? stones.front() : 0;
  }
};

int main() {}