File tree Expand file tree Collapse file tree 1 file changed +8
-10
lines changed
heap/kth-largest-element-in-an-array Expand file tree Collapse file tree 1 file changed +8
-10
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ Brute Force
8
8
/**
9
9
* Question : 215. Kth Largest Element in an Array
10
10
* Complexity : Time: O(nlog(n)) ; Space: O(n)
11
- * Topics : array
11
+ * Topics : Heap
12
12
*/
13
13
class Solution {
14
14
public int findKthLargest (int [] nums , int k ) {
@@ -25,8 +25,8 @@ Quick Select
25
25
``` java
26
26
/**
27
27
* Question : 215. Kth Largest Element in an Array
28
- * Complexity : Time: O(n^2 ) ; Space: O(1)
29
- * Topics : array
28
+ * Complexity : Time: O(n) ; Space: O(1)
29
+ * Topics : Heap
30
30
*/
31
31
class Solution {
32
32
public int findKthLargest (int [] nums , int k ) {
@@ -37,8 +37,8 @@ class Solution {
37
37
}
38
38
39
39
private int quickSelect (int [] nums , int low , int high , int targetIndex ) {
40
- if (low == high) {
41
- return nums[low] ;
40
+ if (low > high) {
41
+ return Integer . MIN_VALUE ;
42
42
}
43
43
44
44
int pi = partition(nums, low, high);
@@ -58,12 +58,10 @@ class Solution {
58
58
int i = low;
59
59
int j = low;
60
60
61
- while ( j < high) {
61
+ for (; j < high; j ++ ) {
62
62
if (nums[j] < pivot) {
63
- swap(nums, i, j);
64
- i++ ;
63
+ swap(nums, i++ , j);
65
64
}
66
- j++ ;
67
65
}
68
66
swap(nums, i, high);
69
67
86
84
/**
87
85
* Question : 215. Kth Largest Element in an Array
88
86
* Complexity : Time: O(nlog(k)) ; Space: O(1)
89
- * Topics : array
87
+ * Topics : Heap
90
88
*/
91
89
class Solution {
92
90
public int findKthLargest (int [] arr , int k ) {
You can’t perform that action at this time.
0 commit comments