for Robot Artificial Inteligence

55. Last Stone Weight

|

priority_queue

class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int> pq(begin(stones),end(stones)); // 즉 오름차순으로 정렬(112478)

        while(pq.size()>1)
        {
            int x = pq.top();
            pq.pop();
            int y = pq.top();
            pq.pop();
            if(x>y)
                pq.push(x-y);
        }
        return pq.empty() ? 0 : pq.top();

    }
};

Comments