problem:2379. 得到 K 个黑块的最少涂色次数
思路:定长滑动窗口模板题
Accode:
class Solution {
public:
int minimumRecolors(string blocks, int k) {
deque<char> deq;
int cnt = 0;
for(int i=0;i<blocks.length();i++){
if(deq.size() && i-deq.front()+1>k) deq.pop_front();
if(blocks[i]=='B') deq.push_back(i);
if(i+1>=k){
cnt = max(cnt,(int)deq.size());
}
}
return k-cnt;
}
};
时间复杂度:$o(n)$