-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0215 数组中的第K个最大元素 - 堆 - 自实现.cpp
More file actions
68 lines (60 loc) · 1.51 KB
/
0215 数组中的第K个最大元素 - 堆 - 自实现.cpp
File metadata and controls
68 lines (60 loc) · 1.51 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
template <typename Comparator>
class my_priority_queue {
public:
vector<int> v;
Comparator cmp;
my_priority_queue() : v() {
v.push_back(-1);
};
void push(const int & t) {
v.push_back(t);
int c_idx = v.size()-1;
int p_idx = c_idx/2;
while (c_idx > 1 && cmp(v[p_idx], v[c_idx])) {
swap(v[p_idx], v[c_idx]);
c_idx = p_idx;
p_idx = c_idx/2;
}
}
int get_vip_chiled_idx(int p_idx) {
int c_idx = p_idx * 2;
if (c_idx<v.size()-1 && cmp(v[c_idx], v[c_idx+1]))
++c_idx;
return c_idx;
}
int pop() {
int root_val = v[1];
int back_val = v[v.size()-1];
swap(v[1], v[v.size()-1]);
v.pop_back();
int p_idx = 1;
int c_idx = get_vip_chiled_idx(p_idx);
while (c_idx<v.size() && cmp(v[p_idx], v[c_idx])) {
swap(v[p_idx], v[c_idx]);
p_idx = c_idx;
c_idx = get_vip_chiled_idx(p_idx);
}
return root_val;
}
int top() {
return v[1];
}
int size() {
return v.size()-1;
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
my_priority_queue<greater<int>> pq; // greater: min heap
for (auto const & n : nums) {
if (pq.size()==k)
if (pq.top() >= n)
continue;
else
pq.pop();
pq.push(n);
}
return pq.top();
}
};