精華區beta Marginalman 關於我們 聯絡資訊
※ 引述 《JIWP (神楽めあ的錢包)》 之銘言: :   : 今天是第365天寫每日 :   : 一年了 有夠快 :   : 2516. Take K of Each Character From Left and Right :   : 有一個長度為n的字串s是由a、b、c組成的 :   : 每一分鐘可以從最左邊或是最右邊拿走一個字母 :   : 請問最少要幾分鐘才可以每個字母最少拿到k個? :   : 思路 : :   : 就sliding window + hash table :   :   我跟jiwp寶的思路差不多 但是差在我明天要回台北了 所以連勝大概又要斷了 嗚嗚哇哇哇哇 ```cpp class Solution { public: int takeCharacters(string s, int k) { if(k == 0)return 0; int n = s.size(); int res = 0; int l = 0; vector<int> save(3,0); for(int i = 0 ; i < n ; i ++) { save[s[i]-'a'] ++; } for(int c = 0; c < 3 ; c ++)if(save[c] < k)return -1; for(int i = 0 ; i < n ; i ++) { save[s[i]-'a'] --; while(l <= i && save[s[i]-'a'] < k) { save[s[l]-'a'] ++; l++; } res = max(res ,i-l+1); // cout << i-l << " " << ok << endl; } return n - res; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.167.193 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1732112151.A.43A.html
dont: 大師 11/21 01:29