看板 Marginalman
忘記C++的pq是max_heap了 花了我一點時間debug 一二三四五 == class KthLargest { public: int k; priority_queue<int, vector<int>, greater<int>> pq; KthLargest(int k, vector<int>& nums) { this->k = k; for(auto num : nums) { this->pq.push(num); while (this->pq.size() > k) { this->pq.pop(); } } } int add(int val) { this->pq.push(val); while (this->pq.size() > this->k) { this->pq.pop(); } return this->pq.top(); } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723478224.A.C4C.html ※ 編輯: DJYOMIYAHINA (125.229.37.69 臺灣), 08/12/2024 23:57:23
Smallsh: 大師 08/12 23:57