-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_1.java
More file actions
42 lines (37 loc) · 802 Bytes
/
leetcode_1.java
File metadata and controls
42 lines (37 loc) · 802 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
import java.util.PriorityQueue;
public class leetcode_1 {
public static void minheap(int [] arr) {
int prevIndex = -1;
int count=0;
int finalCount = 0;
int finalIndex = -1;
PriorityQueue<Integer> heap = new PriorityQueue();
for(int i=0; i<arr.length; i++) {
heap.add(arr[i]);
}
for(int i=0; i<arr.length; i++) {
int p = heap.peek();
if(prevIndex==-1) {
prevIndex=p;
count++;
}
else if(p==prevIndex) {
count++;
if(count>finalCount) {
finalCount = count;
finalIndex=prevIndex;
}
}
else {
count=1;
prevIndex=p;
}
heap.poll();
}
System.out.println(finalIndex);
}
public static void main(String[] args) {
int [] arr = {2,2,1,1,1,2,2};
leetcode_1.minheap(arr);
}
}