-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1046.cpp
More file actions
30 lines (30 loc) · 849 Bytes
/
1046.cpp
File metadata and controls
30 lines (30 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution
{
public:
int lastStoneWeight(vector<int>& stones)
{
/*
* 优先队列 : priority_queue
* 对于基础类型 默认是大根堆(降序队列) : priority_queue<int> pq;
* 小根堆(升序队列) : priority_queue<int, vector<int>, greater<int> > pq;
*/
priority_queue<int> record;
for(int weight : stones) {
record.push(weight);
}
while(! record.empty())
{
if(record.size() == 1)
return record.top();
else {
int max1 = record.top();
record.pop();
int max2 = record.top();
record.pop();
if(abs(max1 - max2) != 0)
record.push(max1 - max2);
}
}
return 0;
}
};