Repository navigation
Expand file tree
/
Copy pathKLargest.java
More file actions
77 lines (63 loc) · 2.06 KB
/
Copy pathKLargest.java
File metadata and controls
77 lines (63 loc) · 2.06 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
package com.test;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created by carmentano on 03/06/2018.
*/
public class KLargest {
public static void main(String[] args) {
int[] v = {3,2,3,1,2,4,5,5,6};
int kthLargest = new KLargest().findKthLargest(v, 9);
kthLargest = new KLargest().findKthLargest(v, 8);
kthLargest = new KLargest().findKthLargest(v, 7);
kthLargest = new KLargest().findKthLargest(v, 6);
kthLargest = new KLargest().findKthLargest(v, 5);
kthLargest = new KLargest().findKthLargest(v, 4);
kthLargest = new KLargest().findKthLargest(v, 3);
kthLargest = new KLargest().findKthLargest(v, 2);
kthLargest = new KLargest().findKthLargest(v, 1);
// int aux = new KLargest().findAux(v, 0, v.length - 1, 5);
int a = 1;
}
public int findKthLargest2(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
public int findKthLargest(int[] nums, int k) {
return findAux(nums, 0, nums.length-1, nums.length-k);
}
private int findAux(int[] nums, int start, int end, int idx) {
if(start == end) {
return nums[start];
} else {
int pivot = nums[start];
int posPivot = start;
int i = start + 1;
int j = end;
while(i <= j) {
while(i <= j && nums[i] <= pivot) {
i++;
}
while(i <= j && nums[j] > pivot) {
j--;
}
if (i <= j) {
swap(nums, i, j);
}
}
swap(nums, posPivot, j);
if(j == idx) {
return nums[idx];
} else if (j < idx) {
return findAux(nums, j+1, end, idx);
} else {
return findAux(nums, start, j - 1, idx);
}
}
}
private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}