Re: [閒聊] 每日leetcode

看板Marginalman作者 (是oin的說)時間1年前 (2024/08/13 00:02), 編輯推噓3(302)
留言5則, 5人參與, 1年前最新討論串702/1553 (看更多)
題目: 做一個會回傳第k個最大的數字的東西 思路: priority queue 小的是top的順序 放K個東西 這樣top就會是第K個最大的東西了 然後 queue的前面是front priority queue 的前面是top 明明名字很像可是不一樣 我知道是因為pq背後原理是樹 可是還是會搞錯 很靠北 ```cpp class KthLargest { public: priority_queue<int,vector<int> ,greater<int>> save; KthLargest(int k, vector<int>& nums) { save.push(-999999); for(int k : nums) { save.push(k); } while(save.size() > k) { save.pop(); } } int add(int val) { save.push(val); save.pop(); return save.top(); } }; /** * Your KthLargest object will be instantiated and called as such: * KthLargest* obj = new KthLargest(k, nums); * int param_1 = obj->add(val); */ ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.17.100 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723478573.A.669.html

08/13 00:02, 1年前 , 1F
大師
08/13 00:02, 1F

08/13 00:06, 1年前 , 2F
大師
08/13 00:06, 2F

08/13 00:07, 1年前 , 3F
你有甚麼用
08/13 00:07, 3F

08/13 00:08, 1年前 , 4F
你有甚麼用
08/13 00:08, 4F

08/13 15:26, 1年前 , 5F
大師
08/13 15:26, 5F
文章代碼(AID): #1ckZ8jPf (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ckZ8jPf (Marginalman)