Skip to content

Commit 8b1470e

Browse files
committed
Updated kth largest
1 parent 7122a3d commit 8b1470e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Other/KthLargest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public static void main(String[] args) {
1111
// int[] A = { 1, 3, 5, 2, 4, 6 };
1212
int[] A = {1, 23, 12, 9, 30, 2, 50};
1313
for (int k = 1; k <= A.length; k++) {
14-
// System.out.println(K.findKthLargest(A, k));
15-
System.out.println(K.findKthLargestB(A, k));
14+
System.out.println(K.findKthLargest(A, k));
15+
// System.out.println(K.findKthLargestB(A, k));
1616
}
1717
}
1818

@@ -32,6 +32,10 @@ public int findKthLargest(int[] A, int k) {
3232
/**
3333
* QuickSelect
3434
* Use partition algorithm in Quick Sort
35+
* Compare partition index with k - 1
36+
* If index > k - 1, means upper bound can be index - 1
37+
* If index < k - 1, means lower bound can be index + 1
38+
* If index == k - 1, return that number
3539
*/
3640
public int findKthLargestB(int[] A, int k) {
3741
if (k <= 0 || k > A.length) return -1;
@@ -59,7 +63,7 @@ public int findKthLargestB(int[] A, int k) {
5963
* Swap and move on
6064
* Return left pointer
6165
*/
62-
private static int partition(int[] a, int left, int right) {
66+
private int partition(int[] a, int left, int right) {
6367
int pivot = a[left + (right - left) / 2];
6468
while(left <= right) {
6569
while(a[left] > pivot) left++;

0 commit comments

Comments
 (0)