605. Can Place Flowers
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array
flowerbedcontaining0's and1's, where0means empty and1means not empty, and an integern, returntrueifnnew flowers can be planted in theflowerbedwithout violating the no-adjacent-flowers rule andfalseotherwise.Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: trueExample 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0
i = 0
while i < len(flowerbed):
if flowerbed[i] == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0):
flowerbed[i] = 1
count += 1
if count >= n:
return True
i += 1
return count >= n