aboutsummaryrefslogtreecommitdiffstats
path: root/703/main.cpp
blob: 9d4f5c662ad336d0188a8040fa2f07a5a7ccd6bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "header.hpp"
#include <functional>
#include <queue>

class KthLargest {
public:
  KthLargest(int k, std::vector<int> &nums) {
    this->k = k;
    for (auto &iter : nums) {
      pq.push(iter);
    }

    while (pq.size() > k) {
      pq.pop();
    }
  }

  int add(int val) {
    pq.push(val);
    if (pq.size() > k) {
      pq.pop();
    }
    return pq.top();
  }

private:
  std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
  int k;
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */

int main(int argc, char **argv) { return 0; }