Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 3, 2019 07:00
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 jianminchen/30f1744008976dde303e8539db5d9225 to your computer and use it in GitHub Desktop.
Save jianminchen/30f1744008976dde303e8539db5d9225 to your computer and use it in GitHub Desktop.
Leetcode weekly contest 126 - source code top ranking no. 4
class Solution {
public:
int longestOnes(vector<int>& A, int K) {
int fr = 0, cnt = 0, ans = 0;
for (int i = 0; i < A.size(); i++) {
if (A[i] == 0)
cnt++;
while (cnt > K) {
if (A[fr] == 0)
cnt--;
fr++;
}
ans = max(ans, i + 1 - fr);
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment