-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path215. Kth Largest Element in an Array.cpp
113 lines (108 loc) · 3.53 KB
/
215. Kth Largest Element in an Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class Solution {
public:
// nth_elements : 4 ms
int findKthLargest1(vector<int>& nums, int k) {
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), greater<int>());
return nums[k - 1];
}
// partial_sort : 15ms
int findKthLargest2(vector<int>& nums, int k) {
partial_sort(nums.begin(), nums.begin() + k, nums.end(), greater<int>());
return nums[k - 1];
}
// min heap : 4 ms
int findKthLargest3(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int num : nums) {
pq.push(num);
if (pq.size() > k) {
pq.pop();
}
}
return pq.top();
}
// min heap multiset : 4 ms
int findKthLargest4(vector<int>& nums, int k) {
multiset<int> mset;
for (int num : nums) {
mset.insert(num);
if (mset.size() > k) {
mset.erase(mset.begin());
}
}
return *mset.begin();
}
//https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60309/C%2B%2B-STL-partition-and-heapsort
// check randPiot solutoin :https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60309/C++-STL-partition-and-heapsort/61538
// quick select : 169ms
int findKthLargest5(vector<int>& nums, int k) {
int left = 0, right = nums.size() - 1, kth;
while (true) {
int idx = partition4(nums, left, right);
if (idx == k - 1) {
kth = nums[idx];
break;
}
if (idx < k - 1) {
left = idx + 1;
} else {
right = idx - 1;
}
}
return kth;
}
int partition4(vector<int>& nums, int left, int right) {
int pivot = nums[left], l = left + 1, r = right;
while (l <= r) {
if (nums[l] < pivot && nums[r] > pivot) {
swap(nums[l++], nums[r--]);
}
if (nums[l] >= pivot) {
l++;
}
if (nums[r] <= pivot) {
r--;
}
}
swap(nums[left], nums[r]);
return r;
}
// check https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained/267106
// just copied it to check if it worked
int partition1(vector<int>& nums, int lo, int hi) {
int pivot = nums[hi];
int i = lo;
for (int j = lo; j < hi; j++) {
if (nums[j] <= pivot) {
swap(nums[i], nums[j]);
i++;
}
}
swap(nums[i], nums[hi]);
return i;
}
// ********
//https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/893847/C%2B%2B-Ten-Solutions
// maybe to memorize
int findKthLargest(vector<int>& nums, int k) {
//partition rule: >=pivot pivot <=pivot
int left=0,right=nums.size()-1,idx=0;
while(1){
idx = partition(nums,left,right);
if(idx==k-1) break;
else if(idx < k-1) left=idx+1;
else right= idx-1;
}
return nums[idx];
}
int partition(vector<int>& nums,int left,int right){//hoare partition
int pivot = nums[left], l=left+1, r = right;
while(l<=r){
if(nums[l]<pivot && nums[r]>pivot) swap(nums[l++],nums[r--]);
if(nums[l]>=pivot) ++l;
if(nums[r]<=pivot) --r;
}
swap(nums[left], nums[r]);
return r;
}
};