425. Can Place Flowers
You tend a long strip of garden plots described by an array flowerbed of 0s and 1s: 1 means a flower already grows in that plot, 0 means the plot is empty. The garden rule is strict — no two flowers may ever sit in adjacent plots (the existing bed already respects this).
Given the integer n, return true if you can plant n new flowers somewhere in the bed without ever violating the adjacency rule, and false otherwise.
Example 1:
Input: flowerbed = [1, 0, 0, 0, 1], n = 1
Output: true
Explanation: The middle plot (index 2) has empty plots on both sides, so one flower fits there.
Example 2:
Input: flowerbed = [1, 0, 0, 0, 1], n = 2
Output: false
Explanation: Only index 2 is plantable; after using it, every remaining empty plot touches a flower. Two new flowers do not fit.
Constraints:
- 1 ≤ flowerbed.length ≤ 2 * 10⁴
- flowerbed[i] is 0 or 1
- No two flowers in the initial bed are adjacent
- 0 ≤ n ≤ flowerbed.length
Hints:
A single plot i is plantable exactly when flowerbed[i] is 0 and both neighbors are 0 too — and a neighbor that falls off either end of the array counts as empty.
Scan left to right and plant in every legal plot as soon as you see it. Taking the earliest legal plot never costs you a better arrangement later, so just count how many you manage to place and compare with n.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: flowerbed = [1, 0, 0, 0, 1], n = 1
Expected output: true