-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathProgram.java
45 lines (33 loc) · 1.13 KB
/
Program.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
package AlgoExSolutions.Easy.FindThreeLargestNumbers;
// import java.util.*;
/**
* * Find Three Largest Numbers
*/
class Program {
private static void shiftAndUpdate(int[] res, int num, int index) {
for (int i = 0; i <= index; i++)
if (i == index) res[i] = num;
else res[i] = res[i + 1];
}
private static void updateResult(int[] res, int num) {
if (num > res[2]) shiftAndUpdate(res, num, 2);
else if (num > res[1]) shiftAndUpdate(res, num, 1);
else if (num > res[0]) shiftAndUpdate(res, num, 0);
return;
}
public static int[] findThreeLargestNumbers(int[] array) {
// Write your code here.
if (array == null || array.length < 3) return null;
// int k = 3;
// int[] res = new int[3];
// PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
// for (int el : array)
// maxHeap.add(el);
// for (int i=k-1; i>=0; i--)
// res[i] = maxHeap.poll();
// return res;
int[] res = new int[] {Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE};
for (int el : array) updateResult(res, el);
return res;
}
}