Re: [閒聊] 每日leetcode

看板Marginalman作者 (6B)時間1月前 (2025/10/22 21:11), 編輯推噓0(002)
留言2則, 1人參與, 1月前最新討論串1539/1548 (看更多)
3347. 阿不是 靠腰喔 直接貼過來就ac了 hard也能水==? ※ 引述《sixB (6B)》之銘言: : 3346. : 好感動 : 去年的我一整排wa : 今年的我ac了>< : 當meeting room 在寫 : 看起來是可以不用開map比較快ㄛ : class Solution { : public: : int maxFrequency(vector<int>& nums, int k, int op) { : ranges::sort(nums); : unordered_map<int, int> cnt; : for(int i: nums) { : cnt[i]++; : } : map<int, int> mp; : for(auto [i, c] : cnt){ : mp[i-k] += c; : mp[i]; : mp[i+k+1] -= c; : } : int ma = 0, cur = 0, cntt = 0; : for(auto [i, c]: mp){ : cur += c; : //cout << i << " " << c << " " << cur << " " << ma << '\n'; : if(cur == ma){ : if(cnt[i] > cntt){ : cntt = cnt[i]; : } : } : else if(cur > ma){ : int t = min(cnt[i] + op, cur); : if(t > ma) { : ma = t; : cntt = cnt[i]; : } : } : } : return ma; : } : }; : ----- : Sent from JPTT on my iPad ----- Sent from JPTT on my iPad -- 很姆的咪 姆之咪 http://i.imgur.com/5sw7QOj.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.135.99.218 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1761138690.A.3C5.html

10/22 21:25, 1月前 , 1F
我有看到一個O n logn的很屌的
10/22 21:25, 1F

10/22 21:25, 1月前 , 2F
先sort 然後用兩個指針指上下界 遍歷同時保留指針
10/22 21:25, 2F
文章代碼(AID): #1e-DW2F5 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1e-DW2F5 (Marginalman)