1046_LastStoneWeight - a920604a/leetcode GitHub Wiki
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();
pq.push(a-b);
}
return pq.top();
}
};
- option 1 - heap
- time complexity
O(nlogn)
- space complexity
O(n)
- time complexity
- option 2 - sort
- time complexity
O(n)
- space complexity
O(1)
- time complexity