Put special items to list, Then we don't need handle special case.


  • 0
    M

    When do https://leetcode.com/contest/leetcode-weekly-contest-35/problems/can-place-flowers/ I came up this idea. You see 3 empty place can put a flower.

    class Solution:
        def canPlaceFlowers(self, flowerbed, n):
            """
            :type flowerbed: List[int]
            :type n: int
            :rtype: bool
            """
            if n == 0 :
                return True
    
            # add  10 at first
            # add  01 at last
            connect_0 = 1
    
            for index, f in enumerate(flowerbed + [0, 1]):
                index += 1
                if f == 0:
                    connect_0 += 1
                else:
                    connect_0 = 0
    
                if connect_0 == 3:
                    n = n - 1
                    connect_0 = 1
    
                if n == 0:
                    return True
    
            return n <= 0
    

Log in to reply
 

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