Share my java O(n) time and O(1) space solution


  • 0
    W
    public int findMaxConsecutiveOnes(int[] nums) {
        int precount = -1;
        int count = 0;
        int max = 0;
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] == 1) {
                count++;
            } else {
                max = Math.max(max, precount + count + 1);
                precount = count;
                count = 0;
            }
        }
        max = Math.max(max, precount + count + 1);
        return max;
    }
    

    Compute every consecutive 1s.


Log in to reply
 

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