Python super simple and easy to understand (9 lines). one pass O(n) time and O(1) space


  • 0
    J
    class Solution(object):
    	def findMaxConsecutiveOnes(self, nums):
    		"""
    		:type nums: List[int]
    		:rtype: int
    		"""
    		maximum, seqLen, lastZero = 0, 0, -1
    		for i, n in enumerate(nums):
    			if n == 1:
    				seqLen += 1
    			else:
    				seqLen = i - lastZero
    				lastZero = i
    			maximum = max(maximum, seqLen)
    		return maximum
    

Log in to reply
 

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