Skip to content

Commit 10199a1

Browse files
authored
Create 703. Kth Largest Element in a Stream1 (#555)
2 parents 6aede80 + 776c1b6 commit 10199a1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

703. Kth Largest Element in a Stream1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class KthLargest {
2+
public:
3+
int k;
4+
priority_queue<int> pq;
5+
KthLargest(int k, vector<int>& nums) {
6+
this->k = k;
7+
for (int num : nums)
8+
add(num);
9+
}
10+
11+
int add(int val) {
12+
pq.push(-val);
13+
if (pq.size() > k)
14+
pq.pop();
15+
return -pq.top();
16+
}
17+
};

0 commit comments

Comments
 (0)