We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d843eb5 commit 00dd55eCopy full SHA for 00dd55e
0703-kth-largest-element-in-a-stream/0703-kth-largest-element-in-a-stream.cpp
@@ -0,0 +1,31 @@
1
+class KthLargest {
2
+public:
3
+ int k;
4
+ vector<int> nums;
5
+ KthLargest(int k, vector<int>& nums) {
6
+ this->k = k;
7
+ sort(nums.rbegin(), nums.rend());
8
+ for (int i = 0; i < k && i < nums.size(); i++) {
9
+ this->nums.push_back(nums[i]);
10
+ }
11
12
+ int add(int val) {
13
+ if (nums.size() < k) {
14
+ nums.push_back(val);
15
+ if (nums.size() == k) {
16
17
18
+ } else if (val > nums.back()) {
19
+ nums.pop_back();
20
21
22
23
+ return nums.back();
24
25
+};
26
+
27
+/**
28
+ * Your KthLargest object will be instantiated and called as such:
29
+ * KthLargest* obj = new KthLargest(k, nums);
30
+ * int param_1 = obj->add(val);
31
+ */
0 commit comments