Java AC solution handled the following case


  • 0
    S
        public int findMaxConsecutiveOnes(int[] nums) {
            if (nums == null || nums.length == 0) return 0;
            
            int countAll = 0;
            int countWithoutFlip = 0;
            boolean flipped = false;
            
            int max = 0;
            for (int num :  nums) {
                if (num == 0) {
                    max = Math.max(countAll, max);
                    if (flipped) {
                        countAll = countWithoutFlip + 1;
                        countWithoutFlip = 0;
                    } else {
                        countAll++;
                        countWithoutFlip = 0;
                        flipped = true;
                    }
                    
                } else {
                    countAll++;
                    countWithoutFlip++;
                }
            }
            
            max = Math.max(countAll, max);
            return max;
        }
    }
    

Log in to reply
 

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