class Solution { public int longestOnes(int[] A, int K) { int n = A.length; int res = 0, rest = K; int l = 0, r = 0; while(r < n) { int num = A[r++]; if(num == 1) { res = Math.max(res,r - l); continue; } rest--; while(rest < 0) { num = A[l++]; if(num == 0) rest++; } res = Math.max(res,r - l); } return res; } }