forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 39
/
KthLargestElementInAnArray.cpp
66 lines (57 loc) · 1.88 KB
/
KthLargestElementInAnArray.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
// Source : https://leetcode.com/problems/kth-largest-element-in-an-array/
// Author : Hao Chen
// Date : 2015-06-11
/**********************************************************************************
*
* 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.
*
* For example,
* Given [3,2,1,5,6,4] and k = 2, return 5.
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ array's length.
*
* Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.
*
**********************************************************************************/
class Solution {
public:
//STL using qsort to solve this problem
int findKthLargest_buildin(vector<int>& nums, int k) {
int n=nums.size();
std::nth_element(nums.begin(),nums.end()-k,nums.end());
return nums[n-k];
}
//qsort partition
int partition(vector<int>& nums, int left, int right) {
int pivot = nums[left];
int 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;
}
int findKthLargest_qsort(vector<int>& nums, int k) {
int left = 0, right = nums.size() - 1;
while (true) {
int pos = partition(nums, left, right);
if (pos == k - 1){
return nums[pos];
}
if (pos > k - 1) {
right = pos - 1;
}else{
left = pos + 1;
}
}
}
int findKthLargest(vector<int>& nums, int k) {
return findKthLargest_qsort(nums, k);
}
};