Untitled
unknown
c_cpp
a year ago
636 B
8
Indexable
#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; }
Editor is loading...
Leave a Comment