Leetcode 605 Solution

This article provides solution to leetcode question 605 (can-place-flowers)

https://leetcode.com/problems/can-place-flowers

Solution

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        last = -2

        for i, v in enumerate(flowerbed):
            if v == 1:
                last = i
                continue

            if i - last > 1 and (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0):
                n -= 1
                last = i

        return n <= 0