NlogN using TreeSet


  • 0

    Not the most efficient means, but definitely straightforward:

        public int findMaxConsecutiveOnes(int[] nums) {
            TreeSet<Integer> positions = new TreeSet<>();
            positions.add(-1);
            
            int length = 0;
            for (int ix = 0; ix <= nums.length; ix++) {
                if (ix == nums.length || nums[ix] == 0) {
                    length = Math.max(length, ix - (positions.last() + 1));
                    positions.add(ix);
                }
            }
            
            return length;
        }
    

Log in to reply
 

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