forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
42 lines (31 loc) · 869 Bytes
/
main.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
/// Source : https://leetcode.com/problems/kth-largest-element-in-an-array/
/// Author : liuyubobobo
/// Time : 2018-01-07
#include <iostream>
#include <vector>
#include <cassert>
#include <stdexcept>
#include <ctime>
using namespace std;
/// Sorting
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end(), [](int a, int b){return a > b;});
return nums[k - 1];
}
};
int main() {
vector<int> nums1 = {3, 2, 1, 5, 6, 4};
cout << Solution().findKthLargest(nums1, 2) << endl;
// 5
vector<int> nums2 = {3, 1, 2, 4};
cout << Solution().findKthLargest(nums2, 2) << endl;
// 3
vector<int> nums3 = {3, 3, 3, 3, 3, 3, 3, 3, 3};
cout << Solution().findKthLargest(nums3, 8) << endl;
// 3
return 0;
}