0703.cpp (351B)
1 class KthLargest { 2 priority_queue<int, vector<int>, greater<int>> pq; 3 int k; 4 5 public: 6 KthLargest(int k, vector<int> &nums) : k(k), pq(nums.begin(), nums.end()) { 7 while (pq.size() > k) 8 pq.pop(); 9 } 10 11 int add(int val) { 12 pq.push(val); 13 if (pq.size() > k) pq.pop(); 14 return pq.top(); 15 } 16 };