Advertisement
Guest User

Untitled

a guest
May 25th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. class Solution {
  2. // May 25th 2019
  3. public int longestOnes(int[] arr, int k) {
  4. int maxLen = 0;
  5. int left = 0, right = 0;
  6. int countZeros = 0;
  7. while (right < arr.length) {
  8. if (arr[right] == 0) {
  9. countZeros++;
  10. }
  11. right++;
  12.  
  13. while (left < right && countZeros > k) {
  14. if (arr[left] == 0) {
  15. countZeros--;
  16. }
  17. left++;
  18. }
  19. maxLen = Math.max(maxLen, right - left);
  20. }
  21. return maxLen;
  22. }
  23. }
  24. /*
  25. [1,1,1,0,0,0,1,1,1,1,0]
  26. 2
  27. [1,1,1,1]
  28. 3
  29. [0,0,0,0,0]
  30. 2
  31. [0,1,0,1,1,0]
  32. 0
  33. [0,0,0]
  34. 0
  35. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement