精華區beta Marginalman 關於我們 聯絡資訊
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