Can Place Flowers
Can Place Flowers is LeetCode 605 (Easy). A flowerbed is an array where 1 is a planted plot and 0 an empty one. No two flowers may be in neighbouring plots. Given n, return true if n new flowers can be planted without breaking that rule.
- The starting bed already follows the rule.
- The first and last plots have only one neighbour.
The bed has up to 2 · 10⁴ plots, so one pass is the target.
- 1 <= flowerbed.length <= 2 * 10⁴
- flowerbed[i] is 0 or 1.
- There are no two adjacent flowers in flowerbed.
- 0 <= n <= flowerbed.length
Intuition
Scan from the left and plant at every plot you legally can. That never costs a flower later.
- Why it is safe: everything to the left of plot
iis already decided. Planting atiblocks only ploti + 1. Skippingito plant ati + 1instead would blocki + 2, which is never better. - When a plot is legal: it is empty and both neighbours are empty. A neighbour that does not exist, before the first plot or after the last, counts as empty.
So the greedy scan plants the most flowers possible, and the answer is whether that count reaches n.
A row of slots with a "no two neighbours" rule and a count to reach: take every legal slot from left to right. This greedy scan is safe for the same reason as the greedy scans in Assign Cookies, Non-overlapping Intervals and Jump Game: taking the earliest option never blocks more than skipping it.
Approach
Before reading on: how many flowers fit in [0, 0, 0, 0, 0], and where? Then try [0, 0, 1, 0, 0] with n = 2: what does the first plot's left neighbour count as?
Scan the plots from left to right
Keep a count planted = 0. For each index i, skip the plot if it already holds a flower.
Read both neighbours, treating the ends as empty
For an empty plot, read left and right:
left = flowerbed[i - 1]ifi > 0, else 0.right = flowerbed[i + 1]ifiis not the last index, else 0.
If either one is 1, the plot is blocked; move on. Outside the bed there is no flower, so a missing neighbour counts as empty.
Plant and write it into the bed
Both neighbours are empty, so set flowerbed[i] = 1 and add one to planted. Write the 1 into the bed so the next plot sees the new flower as its left neighbour.
Stop as soon as n flowers fit
If planted >= n, return true straight away; the rest of the bed cannot change the answer. If the scan ends first, return planted >= n.
Can Place Flowers solution in Python | C++ | Java
Common pitfalls
Counting a plot without planting it
if left == 0 and right == 0:
planted += 1if left == 0 and right == 0:
flowerbed[i] = 1
planted += 1The next plot still sees an empty left neighbour and gets counted too, so two neighbouring flowers are counted. On [0, 0, 0] this counts 3 instead of 2, and returns true for n = 3.
Reading neighbours without checking the ends
if flowerbed[i - 1] == 0 and flowerbed[i + 1] == 0:
left = flowerbed[i - 1] if i > 0 else 0 right = flowerbed[i + 1] if i < len(flowerbed) - 1 else 0
At the last index i + 1 is out of range. At index 0, Python reads flowerbed[-1], the last plot, so [0, 0, 1] with n = 1 wrongly refuses plot 0 and returns false, with no error to warn you.
Edge cases
n = 0Nothing needs planting. The scan may plant nothing, and planted >= n is 0 >= 0, so the answer is true.
[0]It has no neighbours at all, so both count as empty and one flower fits.
Complexity
Can Place Flowers FAQ
Can you pad the flowerbed instead of checking the ends?
Yes. Build [0] + flowerbed + [0] and check plots 1 to m: now every real plot has two neighbours, so no bounds test is needed. It costs O(m) extra memory for the copy.
Is it fine to change the input array?
Yes, LeetCode does not check the array afterwards. If you must keep it unchanged, skip the plot after each new flower instead of writing a 1: after planting at i, jump to i + 2, since i + 1 is blocked anyway.
Is there a way to solve it without the greedy scan?
You can count runs of empty plots. A run of k empty plots between two flowers fits (k - 1) // 2 new ones; a run touching one end of the bed fits k // 2; a bed with no flowers at all fits (k + 1) // 2. Add these up and compare with n. It gives the same answer as the scan, with more cases to get right.