Re: [閒聊] 每日leetcode

作者: DJYOMIYAHINA (通通打死)   2024-08-12 23:57:02
忘記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();
}
};
作者: Smallsh (Smallsh)   2023-08-12 23:57:00
大師
作者: rainkaras (rainkaras)   2024-08-13 00:25:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com