Re: [閒聊] 每日leetcode
忘記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
→
08/12 23:57,
1年前
, 1F
08/12 23:57, 1F
推
08/13 00:25,
1年前
, 2F
08/13 00:25, 2F
→
08/13 15:25,
1年前
, 3F
08/13 15:25, 3F
討論串 (同標題文章)
完整討論串 (本文為第 701 之 1554 篇):