Can Place Flowers

Problem Id: 605 Difficulty: Easy


Intuition

Solution


class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        flowerbed = [0] + flowerbed + [0]
        ans = 0
        for i in range(1, len(flowerbed) - 1):
            if flowerbed[i] == 1:
                continue
            if flowerbed[i - 1] == 0 and flowerbed[i + 1] == 0:
                flowerbed[i] = 1
                ans += 1
        return ans >= n