problem:1004. 最大连续1的个数 III
思路:维护滑动窗口中0的个数
Accode:
class Solution {
public:
int longestOnes(vector<int>& nums, int k) {
int ans = 0;
// int len = answerKey.length();
deque<char> deq;
int tot = 0;
for (auto it : nums) {
if(it==0) tot++;
deq.push_back(it);
while(tot>k){
if(deq.front()==0) tot--;
deq.pop_front();
}
ans = max(ans, (int)deq.size());
}
return ans;
}
};
时间复杂度:$o(n)$