-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
48 lines (43 loc) · 843 Bytes
/
Heap.java
File metadata and controls
48 lines (43 loc) · 843 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package algorithmtest;
import java.util.PriorityQueue;
public class Heap {
public static int findKthLargest(int[] nums,int k)
{
PriorityQueue<Integer> q=new PriorityQueue<Integer>(k);
for(int i:nums)
{
q.offer(i);
if(q.size()>k)
{
q.poll();
}
}
return q.peek();
}
class Record {
public int id, score;
public Record(int id, int score){
this.id = id;
this.score = score;
}
}
public static int findKthMin(int[] nums,int k)
{
PriorityQueue<Integer> q=new PriorityQueue<Integer>((a,b)->b-a);
for(int i=0;i<nums.length;i++)
{
q.offer(nums[i]);
if(q.size()>k)
{
q.poll();
}
}
return q.poll();
}
public static void main(String[] args) {
int []num=new int[] {1,2,10,4,5,3};
findKthMin(num,2);
String a ="123";
//a.substring(beginIndex)
}
}