Skip to content

Commit ca2c3cb

Browse files
committed
215 Kth Largest Element in an Array
1 parent 14b7970 commit ca2c3cb

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
+ [210 Course Schedule II(拓扑排序)](algorithms/CourseScheduleII)
122122
+ [211 Add and Search Word - Data structure design(Trie树)](algorithms/AddandSearchWord)
123123
+ [213 House Robber II(DP)](algorithms/HouseRobberII)
124+
+ [215 Kth Largest Element in an Array(快排partition函数)](algorithms/KthLargestElementinanArray)
124125

125126
## Database
126127

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Kth Largest Element in an Array
2+
3+
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
4+
5+
For example,
6+
Given `[3,2,1,5,6,4]` and k = 2, return 5.
7+
8+
Note:
9+
You may assume k is always valid, 1 ≤ k ≤ array's length.
10+
11+
Credits:
12+
Special thanks to @mithmatt for adding this problem and creating all test cases.
13+
14+
## Solution
15+
16+
利用快排法的partition函数:
17+
18+
```cpp
19+
int partition(vector<int> &nums, int s, int t)
20+
{
21+
int i = s, j = t;
22+
int k = nums[s];
23+
while (i < j) {
24+
while (nums[j] <= k && i < j) --j;
25+
if (i < j) {
26+
nums[i] = nums[j];
27+
}
28+
while (nums[i] >= k && i < j) ++i;
29+
if (i < j) {
30+
nums[j] = nums[i];
31+
}
32+
}
33+
nums[i] = k;
34+
return i;
35+
}
36+
```
37+
38+
设返回的位置为pos,这个pos的值一定是排序后的正确顺序,因此若:
39+
40+
* pos == k - 1, 则直接返回pos位置的值即可
41+
* 若pos < k - 1, 则在左边查找
42+
* 否则在右边查找.
43+
44+
```cpp
45+
int findKthLargest(vector<int> &nums, int k) {
46+
int s = 0, t = nums.size() - 1;
47+
while (true) {
48+
int pos = partition(nums, s, t);
49+
if (pos == k - 1)
50+
return nums[pos];
51+
if (pos > k - 1) {
52+
t = pos - 1;
53+
} else {
54+
s = pos + 1;
55+
}
56+
}
57+
return -1;
58+
}
59+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <cstdio>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <vector>
5+
#include <algorithm>
6+
using namespace std;
7+
8+
void print(const vector<int> &nums)
9+
{
10+
for (auto i : nums)
11+
cout << i << " ";
12+
cout << endl;
13+
}
14+
class Solution {
15+
public:
16+
int findKthLargest(vector<int> &nums, int k) {
17+
int s = 0, t = nums.size() - 1;
18+
while (true) {
19+
int pos = partition(nums, s, t);
20+
if (pos == k - 1)
21+
return nums[pos];
22+
if (pos > k - 1) {
23+
t = pos - 1;
24+
} else {
25+
s = pos + 1;
26+
}
27+
}
28+
return -1;
29+
}
30+
private:
31+
int partition(vector<int> &nums, int s, int t)
32+
{
33+
int i = s, j = t;
34+
int k = nums[s];
35+
while (i < j) {
36+
while (nums[j] <= k && i < j) --j;
37+
if (i < j) {
38+
nums[i] = nums[j];
39+
}
40+
while (nums[i] >= k && i < j) ++i;
41+
if (i < j) {
42+
nums[j] = nums[i];
43+
}
44+
}
45+
nums[i] = k;
46+
return i;
47+
}
48+
};
49+
int main(int argc, char **argv)
50+
{
51+
Solution solution;
52+
vector<int> nums = {1,1,1,1};
53+
print(nums);
54+
int result = solution.findKthLargest(nums, 3);
55+
printf("result = %d\n", result);
56+
return 0;
57+
}

0 commit comments

Comments
 (0)