Super simple java solution (beats 80%)


  • 0
        public int findMaxConsecutiveOnes(int[] nums) {
            if (nums == null || nums.length == 0) return 0;
            
            int maxCount = 0, runningCount = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    maxCount = Math.max(maxCount, runningCount);
                    runningCount = 0; // reset running count
                } else {
                    runningCount++; // increment running count
                }
            }
            maxCount = Math.max(maxCount, runningCount); // in case nums ends with a trail of 1s
            return maxCount;
        }
    

    This solution beats other solutions like this one simply because it inverts where it calculates the maxCount (presumably the OJ has input test cases with a large number of 1s that reduces the number of times Math.max(..) has to be called in my code.


Log in to reply
 

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