Skip to content

Commit 4d06263

Browse files
author
rchen102
committed
review 1
1 parent 4c96bdf commit 4d06263

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Java/leetcode 215.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
// Solution1: Divide and Conquer
22
// T:O(n)(best) O(N^2)(worst) S: O(1)
33
class Solution {
4+
// iterative
5+
public int findKthLargest(int[] nums, int k) {
6+
if (nums == null || nums.length == 0) return -1;
7+
int lo = 0, hi = nums.length - 1;
8+
int target = k;
9+
while (lo <= hi) {
10+
int pivot = partition(nums, lo, hi);
11+
if (pivot - lo + 1 == target) return nums[pivot];
12+
else if (pivot - lo + 1 < target) {
13+
target = target-(pivot - lo + 1);
14+
lo = pivot+1;
15+
}
16+
else {
17+
hi = pivot-1;
18+
}
19+
}
20+
return -1;
21+
}
22+
23+
// reursion
424
public int findKthLargest(int[] nums, int k) {
525
if (nums == null || nums.length == 0) return 0;
626
return helper(nums, 0, nums.length - 1, k);

Leetcode record.xlsx

-12.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)