-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
67 lines (57 loc) · 2.21 KB
/
Solution.java
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
class Solution {
class MaxHeap {
ArrayList<Integer> heap;
MaxHeap() {
heap = new ArrayList<Integer>();
}
void push(int value) {
heap.add(value);
int pos = heap.size() - 1;
int pPos = (pos % 2 == 0 ? pos - 2 : pos - 1) / 2;
while (pPos >= 0 && value > heap.get(pPos)) {
heap.set(pos, heap.get(pPos));
heap.set(pPos, value);
pos = pPos;
pPos = (pos % 2 == 0 ? pos - 2 : pos - 1) / 2;
}
}
int size() {
return heap.size();
}
int pop() {
int size = heap.size();
if (size == 0)
throw new ArrayIndexOutOfBoundsException("pop from empty list");
else if (size == 1)
return heap.remove(0);
int value = heap.get(0);
heap.set(0, heap.remove(size - 1));
int pos = 0;
int maxChildPos = pos * 2 + 2;
if (maxChildPos < size - 1)
maxChildPos = heap.get(maxChildPos) > heap.get(maxChildPos - 1) ? maxChildPos : maxChildPos - 1;
else
maxChildPos = maxChildPos - 1 < size - 1 ? maxChildPos - 1 : pos;
while (maxChildPos < size - 1 && heap.get(pos) < heap.get(maxChildPos)) {
int temp = heap.get(maxChildPos);
heap.set(maxChildPos, heap.get(pos));
heap.set(pos, temp);
pos = maxChildPos;
maxChildPos = pos * 2 + 2;
if (maxChildPos < size - 1)
maxChildPos = heap.get(maxChildPos) > heap.get(maxChildPos - 1) ? maxChildPos : maxChildPos - 1;
else
maxChildPos = maxChildPos - 1 < size - 1 ? maxChildPos - 1 : pos;
}
return value;
}
}
public int findKthLargest(int[] nums, int k) {
MaxHeap maxHeap = new MaxHeap();
for (int num : nums)
maxHeap.push(num);
for (int i = 0; i < k - 1; i++)
maxHeap.pop();
return maxHeap.pop();
}
}