Skip to content

Commit 0688da7

Browse files
committed
Solution as on 08-04-2022 02:23 pm
1 parent 8f13c5e commit 0688da7

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+
// 703.✅ Kth Largest Element in a Stream
2+
3+
class KthLargest
4+
{
5+
public:
6+
priority_queue<int, vector<int>, greater<int>> pq;
7+
int size;
8+
KthLargest(int k, vector<int> &nums)
9+
{
10+
size = k;
11+
for (int i = 0; i < nums.size(); ++i)
12+
{
13+
pq.push(nums[i]);
14+
if (pq.size() > k)
15+
pq.pop();
16+
}
17+
}
18+
19+
int add(int val)
20+
{
21+
pq.push(val);
22+
if (pq.size() > size)
23+
pq.pop();
24+
return pq.top();
25+
}
26+
};

0 commit comments

Comments
 (0)