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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 9.8MB, accepted 2025-12-27.
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();
}
};