-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsolve.cpp
53 lines (52 loc) · 1011 Bytes
/
solve.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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(const vector<int> &nums)
{
for (auto i : nums)
cout << i << " ";
cout << endl;
}
class Solution {
public:
int findKthLargest(vector<int> &nums, int k) {
int s = 0, t = nums.size() - 1;
while (true) {
int pos = partition(nums, s, t);
if (pos == k - 1)
return nums[pos];
if (pos > k - 1) {
t = pos - 1;
} else {
s = pos + 1;
}
}
return -1;
}
private:
int partition(vector<int> &nums, int s, int t)
{
int i = s, j = t;
int k = nums[s];
while (i < j) {
while (nums[j] <= k && i < j) --j;
nums[i] = nums[j];
while (nums[i] >= k && i < j) ++i;
nums[j] = nums[i];
}
nums[i] = k;
return i;
}
};
int main(int argc, char **argv)
{
Solution solution;
vector<int> nums = {1,2,1,3,5,5};
print(nums);
int result = solution.findKthLargest(nums, 2);
printf("result = %d\n", result);
return 0;
}