-
Notifications
You must be signed in to change notification settings - Fork 481
/
0215.cpp
45 lines (44 loc) · 1.01 KB
/
0215.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int findKthLargest(vector<int>& nums, int k)
{
int l = 0, r = nums.size() - 1;
while (true)
{
int p = _partition(nums, l, r);
if (p == k - 1) return nums[p];
if (p > k - 1) r = p - 1;
else l = p + 1;
}
}
private:
int _partition(vector<int>& nums, int l, int r)
{
int j = (rand() % (r - l + 1)) + l;
swap(nums[l], nums[j]);
j = l;
for (int i = l + 1; i <= r; ++i)
{
if (nums[i] > nums[l])
{
++j;
swap(nums[i], nums[j]);
}
}
swap(nums[l], nums[j]);
return j;
}
};
int main()
{
vector<int> nums = {3,2,3,1,2,4,5,5,6};
int k = 4;
cout << Solution().findKthLargest(nums, k);
}