-
Notifications
You must be signed in to change notification settings - Fork 1
/
1046_Last_Stone_Weight.cpp
49 lines (36 loc) · 1.1 KB
/
1046_Last_Stone_Weight.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// https://leetcode.com/problems/last-stone-weight/
// https://leetcode.com/problems/last-stone-weight/discuss/1923115/C%2B%2B-max-heap-or-priority-queue-or-Faster-than-100-(-)
#include <bits/stdc++.h>
using namespace std;
int lastStoneWeight(vector<int> &stones)
{
int n = stones.size();
if (n == 1)
return stones[0];
priority_queue<int> max_heap;
// insert all the stones into the max heap
for (int i = 0; i < n; ++i)
{
max_heap.push(stones[i]);
}
// while there is only 1 element left in the heap
while (max_heap.size() != 1)
{
// max of all the stones
int first = max_heap.top();
max_heap.pop(); // remove the max
// second max of all the stones
int sec = max_heap.top();
max_heap.pop(); // remove the second max
// push the difference of the two max into the max heap
max_heap.push(first - sec);
}
// return the last element in the heap
return max_heap.top();
}
int main()
{
vector<int> stones = {2, 7, 4, 1, 8, 1};
cout << lastStoneWeight(stones) << "\n";
return 0;
}