Skip to content

Commit 408ad86

Browse files
committed
update: kth-largest-element-in-an-array
1 parent 8b84b9c commit 408ad86

File tree

1 file changed

+8
-10
lines changed
  • heap/kth-largest-element-in-an-array

1 file changed

+8
-10
lines changed

heap/kth-largest-element-in-an-array/README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Brute Force
88
/**
99
* Question : 215. Kth Largest Element in an Array
1010
* Complexity : Time: O(nlog(n)) ; Space: O(n)
11-
* Topics : array
11+
* Topics : Heap
1212
*/
1313
class Solution {
1414
public int findKthLargest(int[] nums, int k) {
@@ -25,8 +25,8 @@ Quick Select
2525
```java
2626
/**
2727
* 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
3030
*/
3131
class Solution {
3232
public int findKthLargest(int[] nums, int k) {
@@ -37,8 +37,8 @@ class Solution {
3737
}
3838

3939
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;
4242
}
4343

4444
int pi = partition(nums, low, high);
@@ -58,12 +58,10 @@ class Solution {
5858
int i = low;
5959
int j = low;
6060

61-
while (j < high) {
61+
for (; j < high; j++) {
6262
if (nums[j] < pivot) {
63-
swap(nums, i, j);
64-
i++;
63+
swap(nums, i++, j);
6564
}
66-
j++;
6765
}
6866
swap(nums, i, high);
6967

@@ -86,7 +84,7 @@ Heap
8684
/**
8785
* Question : 215. Kth Largest Element in an Array
8886
* Complexity : Time: O(nlog(k)) ; Space: O(1)
89-
* Topics : array
87+
* Topics : Heap
9088
*/
9189
class Solution {
9290
public int findKthLargest(int[] arr, int k) {

0 commit comments

Comments
 (0)