dsa · easy
Can Place Flowers
flowerbed is a row of plots, 0 empty and 1 already planted. Flowers cannot sit in adjacent plots. The input already obeys that rule. Return whether you can plant n additional flowers (or more precisely, at least n) without putting two flowers next to each other and without uprooting an existing one. Planting zero flowers is always allowed when n = 0.
Example
flowerbed = [0, 0, 1, 0, 0], n = 2.
Plot 0 has empty neighbors (the left edge counts as empty), so you can plant there. Plot 4 is the same on the right edge. Those two new flowers are not adjacent. n = 2 is possible → true.
The same bed with n = 3 cannot work: after planting the two edge plots, the remaining empties all touch a flower → false.
flowerbed = [1, 0, 1], n = 1: the middle 0 sits between two flowers → false.
flowerbed = [0], n = 1: a single empty plot can take a flower → true.
## Arguments - flowerbed — row of plots; 0 empty, 1 already planted; no two 1s are adjacent - n — how many extra flowers you want to plant
Constraints
1 <= flowerbed.length <= 4*10^4 flowerbed[i] is 0 or 1 There are no two adjacent 1s in flowerbed 0 <= n <= flowerbed.length Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [0,0,1,0,0] 2 Expected: true
Example 2
Input: [0,0,1,0,0] 3 Expected: false
Example 3
Input: [1,0,1] 1 Expected: false