605. Can Place Flowers

Information

  • Diffculty: Easy

  • Created: 2019-08-28 08:43:15

  • Last Motified: 2019-08-28 08:43:15

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