Skip to content

Commit a693f8e

Browse files
committed
Time: 16 ms (100.00%), Space: 19.8 MB (61.58%) - LeetHub
1 parent 7041c47 commit a693f8e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class KthLargest {
2+
priority_queue<int, vector<int>, greater<int>> pq;// minheap
3+
int k;
4+
public:
5+
KthLargest(int k, vector<int>& nums) {
6+
this->k = k;
7+
for(auto x: nums){
8+
this->add(x);
9+
}
10+
}
11+
12+
int add(int val) {
13+
pq.push(val);
14+
15+
if(pq.size() > k)
16+
pq.pop();
17+
18+
return pq.top();
19+
}
20+
};
21+
22+
/**
23+
* Your KthLargest object will be instantiated and called as such:
24+
* KthLargest* obj = new KthLargest(k, nums);
25+
* int param_1 = obj->add(val);
26+
*/

0 commit comments

Comments
 (0)