https://leetcode.com/problems/last-stone-weight/

1. Use priority queue:

  1. //4 ms 7.6 MB
  2. class Solution {
  3. public:
  4. int lastStoneWeight(vector<int>& A) {
  5. priority_queue<int> pq (A.begin(), A.end());
  6. while (pq.size() > 1) {
  7. int x = pq.top();
  8. pq.pop();
  9. int y = pq.top();
  10. pq.pop();
  11. if (x > y) pq.push(x - y);
  12. }
  13. return pq.empty() ? 0 : pq.top();
  14. }
  15. };