December_day_5

Leetcode December Challenge Day 5 Link to heading

Link to the question

Solution to the question

class Solution(object):
    def canPlaceFlowers(self, flowerbed, n):
        """
        :type flowerbed: List[int]
        :type n: int
        :rtype: bool
        """
        cur=0
        prev=-10
        while cur < len(flowerbed)-1:
            if flowerbed[cur] != 1 and (cur - prev > 1) and (flowerbed[cur+1]) != 1:
                flowerbed[cur] = 1
                prev = cur
                n -= 1
            elif flowerbed[cur] == 1:
                prev = cur
            cur += 1
        if n == 1 and (cur - prev > 1) and flowerbed[cur] != 1:
            return True
        if n > 0:
            return False
        return True