Repository containing solution for #SdeSheetChallenge by striver
Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
class Solution {
public:
int findKthLargest(vector<int>& nums, int k)
{
// min heap
priority_queue<int, vector<int>, greater<int>> pq;
int i = 0, n = nums.size();
while (i < n) {
pq.push(nums[i]);
i++;
if (pq.size() > k)
pq.pop();
}
return pq.top();
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
// Max heap
priority_queue<int> pq;
for(auto x:nums){
pq.push(x);
}
while(--k){
pq.pop();
}
return pq.top();
}
};