Skip to content

Commit 58b9d07

Browse files
committed
Solved problem 703 : Kth Largest Element In A Stream
1 parent b09b8df commit 58b9d07

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Problem: 703
3+
* Name: Kth Largest Element In A Stream
4+
* Difficulty: Easy
5+
* Topic: Priority Queue
6+
* Link: https://leetcode.com/problems/kth-largest-element-in-a-stream/
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
class KthLargest {
13+
private:
14+
// Priority Queue (Min Heap)
15+
priority_queue<int, vector<int>, greater<int>> values;
16+
int size;
17+
18+
public:
19+
// Constructor accepts values but restrains to max k size
20+
KthLargest(int k, vector<int>& nums) {
21+
this->size = k;
22+
for (const int &n : nums){
23+
values.push(n);
24+
if (values.size() > size){
25+
values.pop();
26+
}
27+
}
28+
}
29+
30+
// Adding values works the same as the constructor
31+
int add(int val) {
32+
values.push(val);
33+
if (values.size() > size){
34+
values.pop();
35+
}
36+
return values.top();
37+
}
38+
};

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### Problems Solved
1818

19-
| Total | 41 |
19+
| Total | 42 |
2020
|:---:|:---:|
2121

2222
#### Search By Topic
@@ -36,7 +36,7 @@
3636
| Intervals | 1 |
3737
| Linked Lists | 5 |
3838
| Math & Geometry | 2 |
39-
| Priority Queue | 0 |
39+
| Priority Queue | 1 |
4040
| Sliding Window | 1 |
4141
| Stack | 2 |
4242
| Tries | 0 |
@@ -46,7 +46,7 @@
4646

4747
| Difficulty | Number |
4848
|:---|---:|
49-
| Easy | 40 |
49+
| Easy | 41 |
5050
| Medium | 1 |
5151
| Hard | 0 |
5252

0 commit comments

Comments
 (0)