Java Code with K times change


  • 0
    C
    public int findMaxConsecutiveOnes(int[] nums) {
            int k = 1;//you can change k times
            int l = 0, r = 0;
            int zeroCount = 0;
            int max = 0;
            while(r < nums.length){
                if(zeroCount <= k){
                    if(nums[r] == 0)
                        zeroCount++;
                    r ++;
                }
                if(zeroCount > k){
                    if(nums[l] == 0){
                        zeroCount--;
                    }
                    l ++;
                }
                if(r-l > max){
                    max = r-l;
                }
            }
            return max;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.