Re: [閒聊] 每日leetcode

看板Marginalman作者 (6B)時間4月前 (2025/08/05 00:22), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1491/1548 (看更多)
904. 水果籃 2point刷過去 多退少補 很懶得記所以直接vector^^ class Solution { public: int totalFruit(vector<int>& fruits) { int res = 0; int n = fruits.size(), types = 0; vector<int> cnt(n, 0); for(int l = 0, r = 0, cur = 0; r < n; r++){ if(cnt[fruits[r]] == 0){ while(types >= 2){ cur--; if(--cnt[fruits[l]] == 0) types--; l++; } types++; } cnt[fruits[r]]++; cur++; res = max(res, cur); } return res; } }; -- 很姆的咪 姆之咪 http://i.imgur.com/5sw7QOj.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.135.99.218 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1754324560.A.4B2.html
文章代碼(AID): #1eaDvGIo (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1eaDvGIo (Marginalman)