Skip to content

Commit 9625c4e

Browse files
authored
Create KthLargestElementInAnArray.cpp
1 parent ac41bf4 commit 9625c4e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

medium/KthLargestElementInAnArray.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//215. Kth Largest Element in an Array
2+
class Solution {
3+
public:
4+
int findKthLargest(vector<int>& nums, int k) {
5+
priority_queue<int, vector<int>, greater<>> minHeap;
6+
7+
for (const int num : nums) {
8+
minHeap.push(num);
9+
if (minHeap.size() > k)
10+
minHeap.pop();
11+
}
12+
13+
return minHeap.top();
14+
}
15+
};

0 commit comments

Comments
 (0)