Description

You have a long flowerbed in which some plots are planted and some are not (0 = empty, 1 = planted). Return true if n new flowers can be planted without violating the no-adjacent-flowers rule.

Examples

Input:flowerbed = [1,0,0,0,1], n = 1
Output:true
Explanation:

Can plant at index 2.

Input:flowerbed = [1,0,0,0,1], n = 2
Output:false
Explanation:

Only one empty plot (index 2) has no adjacent flowers. Cannot plant 2 flowers.

Input:flowerbed = [0,0,1,0,0], n = 1
Output:true
Explanation:

Can plant at index 0 or index 4. Both positions have no adjacent flowers (index 0 has no left neighbor and right neighbor is 0; index 4 has no right neighbor and left neighbor is 0).

Constraints

  • 1 ≤ flowerbed.length ≤ 2 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!