Can Place Flowers
EasyP3Coding
A flowerbed is an array of plots: 0 is empty and 1 already holds a flower. Flowers may not be planted in adjacent plots. Return true if n new flowers can be planted without breaking that rule (the existing flowers stay where they are), otherwise false.
Example 1
- Input
- flowerbed = [1,0,0,0,1], n = 1
- Output
- true
- Why
- Plant in the middle plot.
Example 2
- Input
- flowerbed = [1,0,0,0,1], n = 2
- Output
- false
Constraints
- 1 <= flowerbed.length <= 2 * 10^4
- flowerbed[i] is 0 or 1 and no two 1s are adjacent
- 0 <= n <= flowerbed.length
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.