leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1046.cpp (625B)
0 class Solution { 1 public: 2 int lastStoneWeight(vector<int> &stones) { 3 int n = stones.size(); 4 make_heap(stones.begin(), stones.end()); 5 while (stones.size() > 1) { 6 int x, y; 7 pop_heap(stones.begin(), stones.end()); 8 y = stones.back(), stones.pop_back(); 9 pop_heap(stones.begin(), stones.end()); 10 x = stones.back(), stones.pop_back(); 11 if (x != y) { 12 stones.push_back(y - x); 13 push_heap(stones.begin(), stones.end()); 14 } 15 } 16 return !stones.empty() ? stones.back() : 0; 17 } 18 };