Skip to content

Commit 7062852

Browse files
[CREATE] [CPP] 703. Kth Largest Element in a Stream
1 parent 60fe6e7 commit 7062852

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class KthLargest {
2+
public:
3+
priority_queue<int, vector<int>, greater<int>> pq;
4+
int k;
5+
KthLargest(int k, vector<int>& nums) {
6+
this->k = k;
7+
for (auto a: nums) pq.push(a);
8+
while(pq.size()>k){
9+
pq.pop();
10+
}
11+
}
12+
13+
int add(int val) {
14+
pq.push(val);
15+
while(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)