Determine whether at least flowers can be planted in a flowerbed without placing any two flowers in adjacent plots.
You are given a binary array flowerbed where 0 means an empty plot and 1 means a plot already occupied by a flower. You may plant a flower only in an empty plot, and two flowers must never be placed in adjacent plots.
Return whether it is possible to plant at least n new flowers in the flowerbed while respecting the adjacency rule.
Input Format
flowerbed: an array of integers containing only0and1n: a non-negative integer
The flowerbed is treated as a one-dimensional line of plots.
Output Format
Return true if at least n flowers can be planted without violating the no-adjacent-flowers rule; otherwise return false.
Constraints
1 <= flowerbed.length- Each
flowerbed[i]is either0or1 n >= 0- You may only plant in empty plots (
0) - No two planted flowers may be adjacent
Example 1
Input
flowerbed = [1,0,0,0,1], n = 1
Output
true
Explanation
A flower can be planted in the middle empty plot, resulting in [1,0,1,0,1].
Example 2
Input
flowerbed = [1,0,0,0,1], n = 2
Output
false
Explanation
Only one valid planting position exists, so planting two flowers is impossible.
Show 1 more example
Example 3
Input
flowerbed = [0,0,1,0,0], n = 2
Output
true
Explanation
Flowers can be planted at the first and last plots: [1,0,1,0,1].
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.