Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- // May 25th 2019
- public int longestOnes(int[] arr, int k) {
- int maxLen = 0;
- int left = 0, right = 0;
- int countZeros = 0;
- while (right < arr.length) {
- if (arr[right] == 0) {
- countZeros++;
- }
- right++;
- while (left < right && countZeros > k) {
- if (arr[left] == 0) {
- countZeros--;
- }
- left++;
- }
- maxLen = Math.max(maxLen, right - left);
- }
- return maxLen;
- }
- }
- /*
- [1,1,1,0,0,0,1,1,1,1,0]
- 2
- [1,1,1,1]
- 3
- [0,0,0,0,0]
- 2
- [0,1,0,1,1,0]
- 0
- [0,0,0]
- 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement