Can Place Flowers

Easy

flowerbed contains 0s (empty) and 1s (planted). Flowers cannot be planted in adjacent plots.

Return true if n new flowers can be planted without breaking that rule.

Example 1

canPlaceFlowers()
Input
flowerbed = [1,0,0,0,1], n = 1
Output
true

Example 2

canPlaceFlowers()
Input
flowerbed = [1,0,0,0,1], n = 2
Output
false
Constraints
1 <= flowerbed.length <= 2 * 10^4
0 <= n <= flowerbed.length
`flowerbed[i]` is `0` or `1`.

Topics

ArraysGreedy

Implement canPlaceFlowers in the editor and return the answer — printing it is never graded.