-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindKthElement.java
More file actions
79 lines (69 loc) · 1.81 KB
/
Copy pathFindKthElement.java
File metadata and controls
79 lines (69 loc) · 1.81 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
69
70
71
72
73
74
75
76
77
78
79
/*
215. Kth Largest Element in an Array
Medium
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
*/
package heap;
import heap.base.MaxHeap;
import java.util.PriorityQueue;
public class FindKthElement {
/*
思路:
维护一个最小堆,先插入数组前k个值,数组剩下的值,如果比堆顶大,就入堆,最后返回堆顶
*/
public static int findKthLargest(int[] nums, int k) {
if (nums == null || nums.length < k) {
return -1;
}
return findKth1(nums, k);
}
/**
* 用我自己的 最大堆 实现
*
* @param nums
* @param k
* @return
*/
private static int findKth1(int[] nums, int k) {
MaxHeap heap = new MaxHeap();
for (int i = 0; i < nums.length; i++) {
if (i < k) {
heap.add(-nums[i]);
continue;
}
if (-heap.peek() < nums[i]) {
heap.add(-nums[i]);
heap.pop();
}
}
return -heap.peek();
}
/**
* 用java 的priorityQueue实现
*
* @param nums
* @param k
* @return
*/
private static int findKth2(int[] nums, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int i = 0; i < nums.length; i++) {
if (i < k) {
heap.add(nums[i]);
}
if (heap.peek() < nums[i]) {
heap.add(nums[i]);
heap.poll();
}
}
return heap.peek();
}
}