VISUALGO//PROBLEMS
can place flowers · array / stringeasy
flowerbed (plant 1)
1
0
0
0
1
ELEMENTS
flowerbed
n
Python
def canPlaceFlowers(flowerbed: list[int], n: int) -> bool:
count = 0
for i in range(len(flowerbed)):
if flowerbed[i] == 0:
# Check left and right neighbors
left_empty = (i == 0) or (flowerbed[i - 1] == 0)
right_empty = (i == len(flowerbed) - 1) or (flowerbed[i + 1] == 0)
if left_empty and right_empty:
flowerbed[i] = 1
count += 1
if count >= n:
return True
return count >= n