-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path215-kth-largest-element-in-an-array.cpp
More file actions
30 lines (25 loc) · 1.06 KB
/
215-kth-largest-element-in-an-array.cpp
File metadata and controls
30 lines (25 loc) · 1.06 KB
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
// Title: Kth Largest Element in an Array
// Description:
// Given an integer array nums and an integer k, return the k-th largest element in the array.
// Note that it is the k-th largest element in the sorted order, not the k-th distinct element.
// Can you solve it without sorting?
// Link: https://leetcode.com/problems/kth-largest-element-in-an-array/
// Time complexity: O(n*log(k))
// Space complexity: O(k)
class Solution {
public:
int findKthLargest(std::vector<int> &nums, int k) {
assert(1 <= k && k <= nums.size());
// list of top k largest elements (min priority queue)
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
// for each element in the array
for (int num: nums) {
// add the element to the list
pq.push(num);
// discard the smallest element in the list if the size exceeds k
if (pq.size() > k) pq.pop();
}
// the min element of top k largest elements will be the k-th largest element
return pq.top();
}
};