commit 238de164eaeb14e5634fbd1904a81417b3440303
parent 438f235cbc768a2f9e2e2a14db1ea8151bf10f7f
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Wed, 15 Feb 2023 13:46:06 +0100
Random Problem
Diffstat:
2 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/Problems/0703.cpp b/Problems/0703.cpp
@@ -0,0 +1,15 @@
+class KthLargest {
+ priority_queue<int, vector<int>, greater<int>> pq;
+ int k;
+
+public:
+ KthLargest(int k, vector<int> &nums) : k(k), pq(nums.begin(), nums.end()) {
+ while (pq.size() > k) pq.pop();
+ }
+
+ int add(int val) {
+ pq.push(val);
+ if (pq.size() > k) pq.pop();
+ return pq.top();
+ }
+};
diff --git a/README.md b/README.md
@@ -271,6 +271,7 @@ for solving problems.
| 0695 | Medium | [Max Area of Island](Problems/0695.cpp) |
| 0700 | Easy | [Search in a Binary Search Tree](Problems/0700.cpp) |
| 0701 | Medium | [Insert into a Binary Search Tree](Problems/0701.cpp) |
+| 0703 | Easy | [Kth Largest Element in a Stream](Problems/0703.cpp) |
| 0704 | Easy | [Binary Search](Problems/0704.cpp) |
| 0706 | Easy | [Design HashMap](Problems/0706.cpp) |
| 0707 | Medium | [Design Linked List](Problems/0707.cpp) |