Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int longestOnes(int[] nums, int k) {
- int ans = 0; // longest window's length
- int left = 0, right = 0; // start-end position of the window
- int zeros = 0; // number of zeros inside the window
- while(right < nums.length) {
- if (nums[right] == 0) zeros++;
- if (zeros > k) {
- if (nums[left] == 0) zeros--;
- ans = Math.max(ans, right - left);
- left++;
- }
- right++;
- }
- return Math.max(ans, right - left);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement