1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class KthLargest { private: priority_queue<int, vector<int>, greater<int>> q; int k; public: KthLargest(int k, vector<int>& nums) { this->k = k; for (auto& x: nums) add(x); } int add(int val) { q.push(val); if (q.size() > k) q.pop(); return q.top(); } };
|