Skip to content

Commit adbe57f

Browse files
committed
215_Kth_Largest_Element_in_an_Array
1 parent db7c87f commit adbe57f

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Also, there are open source implementations for basic data structs and algorithm
8080
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |
8181
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/206_Reverse_Linked_List.py) | 1. Stack, O(n) and O(n)<br>2. Traverse on prev and curr, then curr.next = prev, O(n) and O(1)<br>3. Recursion, O(n) and O(1) |
8282
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/213_House_Robber_II.py) | f(k) = max(f(k – 2) + num[k], max(dp[0~ls-2],dp[1~ls-1], O(n) and O(1)|
83+
| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/215_Kth_Largest_Element_in_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/215_Kth_Largest_Element_in_an_Array.java) | 1. Sort, O(n) and O(n)<br>2. Heap, O(nlgk) and O(n)<br>3. Quick selection, O(klgn) and O(n)|
8384
| 216 | [Combination Sum III](https://leetcode.com/problems/combination-sum-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/216_Combination_Sum_III.py) | Generate all combinations of length k and keep those that sum to n|
8485
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/217_Contains_Duplicate.py) | 1. Set and compare length<br>2. Sort and check i,i +1|
8586
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/219_Contains_Duplicate_II.py) | 1. Brute force<br> 2. Maintenance a set that contains previous k numbers, and check if curr in set |
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Solution {
2+
public int findKthLargest(int[] nums, int k) {
3+
//https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained
4+
// shuffle nums to avoid n * n
5+
shuffle(nums);
6+
k = nums.length - k;
7+
int lo = 0;
8+
int hi = nums.length - 1;
9+
while (lo < hi) {
10+
final int j = partition(nums, lo, hi);
11+
if(j < k) {
12+
lo = j + 1;
13+
} else if (j > k) {
14+
hi = j - 1;
15+
} else {
16+
break;
17+
}
18+
}
19+
return nums[k];
20+
}
21+
22+
private int partition(int[] a, int lo, int hi) {
23+
24+
int i = lo;
25+
int j = hi + 1;
26+
while(true) {
27+
while(i < hi && less(a[++i], a[lo]));
28+
while(j > lo && less(a[lo], a[--j]));
29+
if(i >= j) {
30+
break;
31+
}
32+
exch(a, i, j);
33+
}
34+
exch(a, lo, j);
35+
return j;
36+
}
37+
38+
private void exch(int[] a, int i, int j) {
39+
final int tmp = a[i];
40+
a[i] = a[j];
41+
a[j] = tmp;
42+
}
43+
44+
private boolean less(int v, int w) {
45+
return v < w;
46+
}
47+
48+
private void shuffle(int a[]) {
49+
final Random random = new Random();
50+
for(int ind = 1; ind < a.length; ind++) {
51+
final int r = random.nextInt(ind + 1);
52+
exch(a, ind, r);
53+
}
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution(object):
2+
# def findKthLargest(self, nums, k):
3+
# """
4+
# :type nums: List[int]
5+
# :type k: int
6+
# :rtype: int
7+
# """
8+
# return sorted(nums, reverse=True)[k - 1]
9+
10+
# def findKthLargest(self, nums, k):
11+
# # build min heap
12+
# heapq.heapify(nums)
13+
# # remove n - k smallest number
14+
# while len(nums) > k:
15+
# heapq.heappop(nums)
16+
# return nums[0]
17+
# #return heapq.nlargest(k, nums)[-1]
18+
19+
def findKthLargest(self, nums, k):
20+
# shuffle nums to avoid n*n
21+
random.shuffle(nums)
22+
return self.quickSelection(nums, 0, len(nums) - 1, len(nums) - k)
23+
24+
def quickSelection(self, nums, start, end, k):
25+
if start > end:
26+
return float('inf')
27+
pivot = nums[end]
28+
left = start
29+
for i in range(start, end):
30+
if nums[i] <= pivot:
31+
# swip left and i
32+
nums[left], nums[i] = nums[i], nums[left]
33+
left += 1
34+
nums[left], nums[end] = nums[end], nums[left]
35+
if left == k:
36+
return nums[left]
37+
elif left < k:
38+
return self.quickSelection(nums, left + 1, end, k)
39+
else:
40+
return self.quickSelection(nums, start, left - 1, k)

python/347_Top_K_Frequent_Elements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ def topKFrequent(self, nums, k):
77
"""
88
counter = collections.Counter(nums)
99
return [k for k,v in counter.most_common(k)]
10-
# return heapq.nlargest(k, count.keys(), key=count.get)
10+
# return heapq.nlargest(k, count.keys(), key=count.get)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy