LeetCode solutions

1127. Last Stone Weight

My accepted C++ solution to LeetCode problem 1127, Last Stone Weight, running in 0ms.

  • Difficulty: Easy
  • C++
  • Runtime 0ms
  • Memory 9.8MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 9.8MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int> pq(stones.begin(), stones.end());
        while (pq.size() > 1) {
            int a = pq.top(); pq.pop();
            int b = pq.top(); pq.pop();
            if (a != b) pq.push(a - b);
        }
        return pq.empty() ? 0 : pq.top();
    }
};

Source