Welcome to Subscribe On Youtube

1004. Max Consecutive Ones III

Description

Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.

 

Example 1:

Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.

Example 2:

Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.

 

Constraints:

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.
  • 0 <= k <= nums.length

Solutions

Same as in https://leetcode.ca/2017-03-31-487-Max-Consecutive-Ones-II/, change from 1 to k for queue size check.

  • class Solution {
        public int longestOnes(int[] nums, int k) {
            int l = 0, r = 0;
            while (r < nums.length) {
                if (nums[r++] == 0) {
                    --k;
                }
                if (k < 0 && nums[l++] == 0) {
                    ++k;
                }
            }
            return r - l;
        }
    }
    
  • class Solution {
    public:
        int longestOnes(vector<int>& nums, int k) {
            int l = 0, r = 0;
            while (r < nums.size()) {
                if (nums[r++] == 0) --k;
                if (k < 0 && nums[l++] == 0) ++k;
            }
            return r - l;
        }
    };
    
  • class Solution:
        def longestOnes(self, nums: List[int], k: int) -> int:
            res, zero, left = 0, 0, 0
    
            for right in range(len(nums)):
                if nums[right] == 0:
                    zero += 1
                while zero > k:
                    if nums[left] == 0:
                        zero -= 1
                    left += 1
                res = max(res, right - left + 1)
    
            return res
    
    
    class Solution_followup:
        def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
    
            res, left, k = 0, 0, 1
            q = deque()
    
            for right in range(len(nums)):
                if nums[right] == 0:
                    q.append(right)
                if len(q) > k:
                    left = q.popleft() + 1
                res = max(res, right - left + 1) # max-check every for iteration
    
            return res
    
    ##############
    
    class Solution:
        def longestOnes(self, nums: List[int], k: int) -> int:
            l = r = -1
            while r < len(nums) - 1:
                r += 1
                if nums[r] == 0:
                    k -= 1
                if k < 0:
                    l += 1
                    if nums[l] == 0:
                        k += 1
            return r - l
    
    
  • func longestOnes(nums []int, k int) int {
    	l, r := -1, -1
    	for r < len(nums)-1 {
    		r++
    		if nums[r] == 0 {
    			k--
    		}
    		if k < 0 {
    			l++
    			if nums[l] == 0 {
    				k++
    			}
    		}
    	}
    	return r - l
    }
    
  • function longestOnes(nums: number[], k: number): number {
        const n = nums.length;
        let l = 0;
        for (const num of nums) {
            if (num === 0) {
                k--;
            }
            if (k < 0 && nums[l++] === 0) {
                k++;
            }
        }
        return n - l;
    }
    
    
  • impl Solution {
        pub fn longest_ones(nums: Vec<i32>, mut k: i32) -> i32 {
            let n = nums.len();
            let mut l = 0;
            for num in nums.iter() {
                if num == &0 {
                    k -= 1;
                }
                if k < 0 {
                    if nums[l] == 0 {
                        k += 1;
                    }
                    l += 1;
                }
            }
            (n - l) as i32
        }
    }
    
    

All Problems

All Solutions