Skip to content

Commit 58b66c5

Browse files
refactor 215
1 parent 9646336 commit 58b66c5

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

src/main/java/com/fishercoder/solutions/_215.java

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Arrays;
34
import java.util.Collections;
45
import java.util.PriorityQueue;
56

@@ -17,15 +18,66 @@
1718
*/
1819
public class _215 {
1920

20-
public int findKthLargest(int[] nums, int k) {
21-
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
22-
for (int i : nums) {
23-
maxHeap.offer(i);
21+
public static class Solution1 {
22+
public int findKthLargest(int[] nums, int k) {
23+
Arrays.sort(nums);
24+
return nums[nums.length - k];
2425
}
25-
while (k-- > 1) {
26-
maxHeap.poll();
26+
}
27+
28+
public static class Solution2 {
29+
public int findKthLargest(int[] nums, int k) {
30+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
31+
for (int i : nums) {
32+
maxHeap.offer(i);
33+
}
34+
while (k-- > 1) {
35+
maxHeap.poll();
36+
}
37+
return maxHeap.poll();
2738
}
28-
return maxHeap.poll();
2939
}
3040

41+
public static class Solution3 {
42+
/**Quick Select algorithm
43+
* Time: O(n) in average, O(n^2) in worst case
44+
*
45+
* Reference: https://discuss.leetcode.com/topic/14611/java-quick-select*/
46+
public int findKthLargest(int[] nums, int k) {
47+
int start = 0;
48+
int end = nums.length - 1;
49+
int index = nums.length - k;
50+
while (start < end) {
51+
int pivot = partition(nums, start, end);
52+
if (pivot < index) start = pivot + 1;
53+
else if (pivot > index) end = pivot - 1;
54+
else return nums[pivot];
55+
}
56+
return nums[start];
57+
}
58+
59+
int partition(int[] nums, int start, int end) {
60+
int pivot = start;
61+
while (start <= end) {
62+
while (start <= end && nums[start] <= nums[pivot]) {
63+
start++;
64+
}
65+
while (start <= end && nums[end] > nums[pivot]) {
66+
end--;
67+
}
68+
if (start > end) {
69+
break;
70+
}
71+
swap(nums, start, end);
72+
}
73+
swap(nums, end, pivot);
74+
return end;
75+
}
76+
77+
void swap(int[] nums, int i, int j) {
78+
int temp = nums[i];
79+
nums[i] = nums[j];
80+
nums[j] = temp;
81+
}
82+
}
3183
}

src/test/java/com/fishercoder/_215Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
* Created by fishercoder on 5/9/17.
1111
*/
1212
public class _215Test {
13-
private static _215 test;
13+
private static _215.Solution2 solution2;
1414
private static int k;
1515
private static int[] nums;
1616
private static int actual;
1717
private static int expected;
1818

1919
@BeforeClass
2020
public static void setup(){
21-
test = new _215();
21+
solution2 = new _215.Solution2();
2222
}
2323

2424
@Test
2525
public void test1(){
2626
k = 2;
2727
nums = new int[]{3,2,1,5,6,4};
28-
actual = test.findKthLargest(nums, k);
28+
actual = solution2.findKthLargest(nums, k);
2929
expected = 5;
3030
assertEquals(expected, actual);
3131
}

0 commit comments

Comments
 (0)