Advertisement
Guest User

Grokking 228

a guest
Jul 27th, 2022
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. class Solution {
  2.     public int longestOnes(int[] nums, int k) {
  3.         int ans = 0; // longest window's length
  4.         int left = 0, right = 0; // start-end position of the window
  5.         int zeros = 0; // number of zeros inside the window
  6.         while(right < nums.length) {
  7.             if (nums[right] == 0) zeros++;
  8.             if (zeros > k) {
  9.                 if (nums[left] == 0) zeros--;
  10.                 ans = Math.max(ans, right - left);
  11.                 left++;
  12.             }
  13.             right++;
  14.         }
  15.        
  16.         return Math.max(ans, right - left);
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement