https://leetcode.com/problems/kth-largest-element-in-a-stream/description/
class KthLargest { public: priority_queue<int> q; int k; KthLargest(int k, vector<int> nums) { this->k = k; for (auto &i : nums) add(i); } int add(int val) { if (q.size() < k) { q.push(-val); } else { if (val > -q.top()) { q.pop(); q.push(-val); } } return -q.top(); } }; /** * Your KthLargest object will be instantiated and called as such: * KthLargest obj = new KthLargest(k, nums); * int param_1 = obj.add(val); */
原文地址:https://www.cnblogs.com/JTechRoad/p/9977760.html
时间: 2024-10-29 19:07:43