A faster python solution.(but why? help me!)


  • 0
    F
    class Solution(object):
        def findMaxConsecutiveOnes(self, nums):
            result=0
            count=0
            for i in nums:
                if i==1:
                    count+=1
                    if result <= count:
                        result =count
                else:
                    count = 0
            return result
    

    It's my answer. And it is faster than the solution below. The difference between the two solutions is the first one using a 'if' sentence and the other 'max' function. And I don't know why the efficiency of 'if' is faster then the 'max'. Help me if you know the reason.

    The other solution:

    class Solution(object):
        def findMaxConsecutiveOnes(self, nums):
            result=0
            count=0
            for i in nums:
                if i==1:
                    count+=1
                    result = max([result,count])
                else:
                    count = 0
            return result
    

Log in to reply
 

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