Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created March 4, 2019 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/b1e8bac929cbfdff9c1bb30371df4653 to your computer and use it in GitHub Desktop.
Save fpdjsns/b1e8bac929cbfdff9c1bb30371df4653 to your computer and use it in GitHub Desktop.
[leetcode] 1004. Max Consecutive Ones III : https://leetcode.com/problems/max-consecutive-ones-iii/
class Solution {
public:
int longestOnes(vector<int>& A, int K) {
int answer = 0;
queue<int> zeroQ;
int start = 0;
for(int end=0;end<A.size();end++){
if(A[end] == 0)
zeroQ.push(end);
if(!zeroQ.empty() && zeroQ.size() > K){
start = zeroQ.front()+1; zeroQ.pop();
}
answer = max(answer, end - start + 1);
}
return answer;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment