Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
636 B
6
Indexable
Never
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int lastStoneWeight(vector<int>& stones) {
    priority_queue<int> maxHeap(stones.begin(), stones.end());

    while (maxHeap.size() > 1) {
        int stoneOne = maxHeap.top();
        maxHeap.pop();
        int stoneTwo = maxHeap.top();
        maxHeap.pop();

        if (stoneOne != stoneTwo) {
            maxHeap.push(stoneOne - stoneTwo);
        }
    }

    return maxHeap.empty() ? 0 : maxHeap.top();
}

int main() {
    vector<int> stones = {1, 3, 2, 6, 7};
    cout << "Last stone weight is: " << lastStoneWeight(stones) << endl;
    return 0;
}
Leave a Comment