File tree 2 files changed +41
-3
lines changed
2 files changed +41
-3
lines changed Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change 16
16
17
17
### Problems Solved
18
18
19
- | Total | 41 |
19
+ | Total | 42 |
20
20
| :---:| :---:|
21
21
22
22
#### Search By Topic
36
36
| Intervals | 1 |
37
37
| Linked Lists | 5 |
38
38
| Math & Geometry | 2 |
39
- | Priority Queue | 0 |
39
+ | Priority Queue | 1 |
40
40
| Sliding Window | 1 |
41
41
| Stack | 2 |
42
42
| Tries | 0 |
46
46
47
47
| Difficulty | Number |
48
48
| :---| ---:|
49
- | Easy | 40 |
49
+ | Easy | 41 |
50
50
| Medium | 1 |
51
51
| Hard | 0 |
52
52
You can’t perform that action at this time.
0 commit comments