Can Place Flowers
Table of Contents + β
Can Place Flowers is a friendly greedy question. The rule is simple. No two flowers can sit next to each other. The interviewer wants to see if you can make the right local choice at each spot and handle the edges of the array cleanly. Those edges are where most people slip.
π― The Problem
You get a flowerbed and a number n. Decide if you can plant n more flowers with no two touching.
- Each box is a plot. A
0is empty. A1already has a flower. - You must plant
nmore flowers. - No two flowers may sit in plots that touch.
- Return
trueif allnfit. Otherwisefalse. - A greedy choice grabs a spot the moment it is safe, without looking far ahead.
Let us say the flowerbed is [1, 0, 0, 0, 1] and n is 1. The middle plot at index 2 is empty. Its neighbors at index 1 and 3 are also empty. So you can plant one flower there. The answer is true.
Input: flowerbed = [1, 0, 0, 0, 1], n = 1Output: true
Explanation: plant at index 2, since index 1 and index 3 are both emptyIf n were 2 for the same bed, the answer would be false. There is just not enough room without two flowers touching.
Here is a picture of the example bed. The middle empty plot has empty neighbors, so it is plantable.
π’ Approach 1: Plant Then Re-Scan (Brute Force)
The idea in one line: plant one flower, scan the whole bed again, repeat.
The idea:
- Find one safe empty plot.
- Plant there and lower
nby one. - Start the scan over from the beginning.
How it works:
- Scan the array for a plot that is empty with empty neighbors.
- Plant a flower there.
- Restart the scan for the next flower.
Why it is weak:
- Every flower you plant makes you scan the bed again.
- In the worst case you scan many times.
- That is O(nΒ²) time. A long bed gets slow.
Here is the brute-force code for that idea:
def can_place_flowers(flowerbed, n): bed = flowerbed[:]
def is_valid(index): left_empty = index == 0 or bed[index - 1] == 0 right_empty = index == len(bed) - 1 or bed[index + 1] == 0 return bed[index] == 0 and left_empty and right_empty
planted = 0 for i in range(len(bed)): if is_valid(i): bed[i] = 1 planted += 1 if planted == n: return True
return planted >= n
print(can_place_flowers([1, 0, 0, 0, 1], 1))β‘ Approach 2: One Greedy Pass (Best)
The idea in one line: walk the bed once and plant at the first safe plot you reach.
The idea:
- Walk the bed left to right one time.
- Plant the moment a plot is safe.
- Never go back.
How it works:
- For each plot, check the plot itself is empty.
- Check the left neighbor is empty or off the edge.
- Check the right neighbor is empty or off the edge.
- Off the edge means before index zero or after the last index, which counts as empty.
- When all three hold, set the plot to
1and lowern. - At the end, return
trueifndropped to zero or below.
Why it is fast:
- One forward pass touches each plot once. That is O(n).
- It changes the bed in place, so no extra array. That is O(1).
- Taking the earliest legal plot never costs a later one, so greedy is safe here.
This picture shows the greedy pass on [1, 0, 0, 0, 1] with n = 1. Follow the decision at each plot.
Steps to Solve
- Walk the flowerbed from the first plot to the last.
- For the current plot, check that it is empty.
- Check that the left neighbor is empty or off the edge.
- Check that the right neighbor is empty or off the edge.
- If all three hold, set the plot to
1and lowernby one. - After the loop, return
trueifnis zero or less, otherwisefalse.
This Python version walks the bed once and treats off-the-edge plots as empty.
def can_place_flowers(bed, n): size = len(bed) for i in range(size): empty = bed[i] == 0 left_free = i == 0 or bed[i - 1] == 0 # edge counts as empty right_free = i == size - 1 or bed[i + 1] == 0 # edge counts as empty if empty and left_free and right_free: bed[i] = 1 # plant here n -= 1 return n <= 0 # true if we planted enough
bed = [1, 0, 0, 0, 1]n = 1print(can_place_flowers(bed, n))The output of the above code will be:
TrueLet us walk through the Python version line by line. Code first, then the why.
def can_place_flowers(bed, n): size = len(bed) for i in range(size): empty = bed[i] == 0 left_free = i == 0 or bed[i - 1] == 0 right_free = i == size - 1 or bed[i + 1] == 0 if empty and left_free and right_free: bed[i] = 1 n -= 1 return n <= 0for i in range(size): walks every plot once, left to right. We never go back. That single forward pass is what makes the greedy method fast.
empty = bed[i] == 0 checks that the current plot has no flower. We can only plant on an empty plot. So this must be true before we even look at the neighbors.
left_free = i == 0 or bed[i - 1] == 0 handles the left side and the left edge in one line. If i is zero, there is no left neighbor, so we treat it as empty. Otherwise the actual left plot must be empty. This i == 0 check is what stops the code from reading before the start of the array.
right_free = i == size - 1 or bed[i + 1] == 0 does the same for the right side. If i is the last index, there is no right neighbor, so it counts as empty. This guard stops the code from reading past the end of the array.
if empty and left_free and right_free: plants only when all three are safe. The plot is empty and both sides are clear. That is the exact rule of the problem.
bed[i] = 1 plants the flower. We change the bed itself, so the next step sees this new flower as the left neighbor. That is why we do not plant two in a row.
n -= 1 lowers how many we still need to plant.
return n <= 0 gives the final answer. If we planted at least as many as asked, n dropped to zero or below, so the answer is true.
β±οΈ Time and Space Complexity
The brute force re-scans the bed for every flower, so its time grows as O(nΒ²). The greedy pass walks the bed just once and makes a safe local choice at each plot. So it is O(n) time. It changes the bed in place, so it uses O(1) extra space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute force (re-scan per flower) | O(nΒ²) | O(1) |
| Single greedy pass | O(n) | O(1) |
Tip
The edges are the trap. The first plot has no left neighbor and the last plot has no right neighbor. Treat those missing neighbors as empty. Say that out loud in the interview so they know you saw the edge case.
π§© Key Takeaways
- β
A
0is an empty plot and a1is a plot that already has a flower. - β You can plant on an empty plot only when both neighbors are empty.
- β Treat positions off the edge of the bed as empty.
- β The greedy choice of planting at the earliest safe plot is always correct.
- β One pass and in-place changes give O(n) time and O(1) extra space.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
When can you plant a flower at a plot?
Why: You can plant only when the plot itself is empty and both neighbors are empty or off the edge.
- 2
How do you treat a neighbor that is off the edge of the bed?
Why: There is nothing off the edge, so a missing neighbor counts as empty.
- 3
Why does the greedy single pass work correctly?
Why: Taking the earliest legal plot is always safe, so one forward pass is enough.
- 4
What is the time and space complexity of the greedy solution?
Why: One pass over the bed is O(n) time, and changing it in place uses O(1) extra space.