leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1046.cpp (625B)


      1 class Solution {
      2   public:
      3     int lastStoneWeight(vector<int> &stones) {
      4         int n = stones.size();
      5         make_heap(stones.begin(), stones.end());
      6         while (stones.size() > 1) {
      7             int x, y;
      8             pop_heap(stones.begin(), stones.end());
      9             y = stones.back(), stones.pop_back();
     10             pop_heap(stones.begin(), stones.end());
     11             x = stones.back(), stones.pop_back();
     12             if (x != y) {
     13                 stones.push_back(y - x);
     14                 push_heap(stones.begin(), stones.end());
     15             }
     16         }
     17         return !stones.empty() ? stones.back() : 0;
     18     }
     19 };