Can Place Flowers (LeetCode 605): Solution and Explanation

Easy
Array
Greedy

A flowerbed is a row of plots, 0 for empty and 1 for planted, and no two flowers may sit in adjacent plots. Given the bed and a number n, decide whether n new flowers can be planted without breaking that rule.

Intuition

A new flower fits in plot i exactly when plot i is empty and both of its neighbors are empty. The ends of the bed have one neighbor missing, and a missing neighbor counts as empty.

Scanning from the left, the first plot that fits is always worth taking. Any plan that leaves it empty and plants further right in the same gap can slide that flower left without blocking anything extra, so planting early never costs a flower.

Once a flower is planted, it becomes the left neighbor of the next plot, which is why the bed must be updated as you go. That stops two new flowers landing side by side.

The answer only asks whether n fit, so the scan can stop the moment the count reaches n.

Approach: Greedy left-to-right planting

  1. Set count = 0.
  2. For each index i, skip it if flowerbed[i] == 1.
  3. Otherwise check left = i == 0 or flowerbed[i - 1] == 0 and right = i == len - 1 or flowerbed[i + 1] == 0.
  4. If both hold, set flowerbed[i] = 1 and increase count. If count >= n, return true right away.
  5. After the loop, return count >= n, which covers n = 0.

Step-by-step example

flowerbed = [1,0,0,0,1], n = 1
  1. Plot 0 already has a flower. Skip it.
  2. Plot 1 is empty, but its left neighbor (plot 0) has a flower, so it is blocked.
  3. Plot 2 is empty, and plots 1 and 3 are both empty. Plant here: count = 1.
  4. count has reached n = 1, so return true without looking at plots 3 and 4.

Code: Python, Java, C++

1class Solution:
2    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
3        count = 0
4        for i in range(len(flowerbed)):
5            if flowerbed[i] == 0:
6                left = i == 0 or flowerbed[i - 1] == 0
7                right = i == len(flowerbed) - 1 or flowerbed[i + 1] == 0
8                if left and right:
9                    flowerbed[i] = 1
10                    count += 1
11                    if count >= n:
12                        return True
13        return count >= n
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each plot is examined once, with constant work per plot.

Space

O(1)

The bed is updated in place and only a counter is kept. Copy the array first if the caller's input must not change.

Other approaches

Count zeros in each gap (time O(n), space O(1))

Split the bed into runs of empty plots. A run of z zeros between two flowers fits (z - 1) / 2 new flowers; a run touching one edge fits z / 2; a bed with no flowers fits (z + 1) / 2. Sum and compare with n.

Pad the bed with zeros (time O(n), space O(n))

Add a 0 at each end so every real plot has two neighbors, then apply the same check without edge cases. Simpler conditions at the cost of a copy.

Common mistakes

  • Forgetting the edges. Plot 0 has no left neighbor and the last plot has no right neighbor; both count as empty.
  • Not writing the new flower back into the bed. Without it, two adjacent empty plots could both be planted.
  • Reading out of bounds at i - 1 or i + 1. Check the edge condition first so the short-circuit protects the index.
  • Returning false for n = 0. Zero new flowers always fit, which count >= n handles.

Frequently asked questions

Why is planting at the first legal plot optimal?

Within a run of empty plots, putting a flower at the leftmost legal spot leaves the most room to its right. Any other plan can be shifted left flower by flower without losing one, so the greedy count is never beaten.

Do I have to modify the input array?

Not necessarily. You can keep a variable for whether the previous plot is now planted, or skip ahead by two after planting. Modifying the array is just the most direct way to make the next check see the new flower.

Why stop early when count reaches n?

The question is yes or no. Once n flowers fit, planting more cannot change the answer, so the rest of the bed does not matter.