-
Notifications
You must be signed in to change notification settings - Fork 0
/
215.cpp
45 lines (37 loc) · 892 Bytes
/
215.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
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
//By Implementing sorting algo.
class Solution {
void mysort(vector<int>& nums, int l, int r)
{
if(l >= r)
return;
int i = r;
for(int j = r-1; j >= l; j--)
{
if(nums[j] >= nums[r])
{
swap(nums[j], nums[i-1]);
i--;
}
}
swap(nums[i], nums[r]);
mysort(nums, l, i-1);
mysort(nums, i+1, r);
}
public:
int findKthLargest(vector<int>& nums, int k) {
mysort(nums, 0, nums.size()-1);
return nums[nums.size()-k];
}
};
//Using STL sort.
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size()-k];
}
};