Skip to content
← All posts

Can Place Flowers: Greedy Planting in a Flowerbed

5 min read
leetcodeproblemeasyarraysgreedy

You have a flowerbed represented as an integer array where 0 means empty and 1 means planted. No two adjacent plots can both have flowers. Given the flowerbed and a number n, return whether you can plant n new flowers without violating the no-adjacent-flowers rule.

This is LeetCode 605: Can Place Flowers, an easy problem that is a great introduction to greedy algorithms on arrays. The solution is short, but the reasoning about boundary conditions is what makes it worth studying.

flowerbed1[0]0[1]0[2]0[3]1[4]validadjacentadjacentn = 1, can plant at index 2 - return true
flowerbed = [1, 0, 0, 0, 1], n = 1. Index 2 is the only valid spot: both neighbors are empty.

Why this problem matters

Can Place Flowers teaches you a fundamental greedy technique: scan left to right, make the locally optimal choice at each position, and trust that it leads to a globally optimal result. The key decision at each cell is simple - if you can plant here, do it immediately. Waiting never helps, because planting earlier never blocks a valid later planting that you would have missed.

This same "greedy placement" pattern shows up in interval scheduling, seat assignment, and resource allocation problems. Once you are comfortable with the adjacency check here, those harder variants become much more approachable.

The approach

Walk through the flowerbed from left to right. At each position, check three conditions:

  1. The current plot is empty (flowerbed[i] == 0)
  2. The left neighbor is empty or we are at the left boundary (i == 0 or flowerbed[i - 1] == 0)
  3. The right neighbor is empty or we are at the right boundary (i == length - 1 or flowerbed[i + 1] == 0)

If all three conditions hold, plant a flower at that position (set flowerbed[i] = 1) and increment your count. By actually modifying the array, you ensure that the next iteration sees the newly planted flower and respects the adjacency constraint.

You can return True early as soon as count >= n. If you finish the scan without reaching that threshold, return whether count >= n.

def canPlaceFlowers(flowerbed, n):
    count = 0
    length = len(flowerbed)

    for i in range(length):
        if flowerbed[i] == 0:
            left_empty = (i == 0) or (flowerbed[i - 1] == 0)
            right_empty = (i == length - 1) or (flowerbed[i + 1] == 0)

            if left_empty and right_empty:
                flowerbed[i] = 1
                count += 1
                if count >= n:
                    return True

    return count >= n

The greedy choice works because planting a flower as early as possible never reduces the number of flowers you can place later. If position i is valid, skipping it and planting at i + 2 instead would give you the same result at best, and potentially fewer flowers overall.

Step-by-step walkthrough

Let's trace through flowerbed = [1, 0, 0, 0, 1] with n = 1.

Step 1: Initial flowerbed state

1[0]0[1]0[2]0[3]1[4]count = 0

flowerbed = [1, 0, 0, 0, 1], n = 1. We need to place 1 flower without violating adjacency rules.

Step 2: Check index 0

1[0]0[1]0[2]0[3]1[4]i = 0count = 0

flowerbed[0] = 1. Already planted. Skip.

Step 3: Check index 1

1[0]0[1]0[2]0[3]1[4]i = 1count = 0

flowerbed[1] = 0, but left neighbor flowerbed[0] = 1. Cannot plant here.

Step 4: Check index 2 - plant a flower

1[0]0[1]1[2]0[3]1[4]i = 2count = 1

flowerbed[2] = 0, left neighbor flowerbed[1] = 0, right neighbor flowerbed[3] = 0. All three conditions met. Plant here. count = 1.

Step 5: Check index 3

1[0]0[1]1[2]0[3]1[4]i = 3count = 1

flowerbed[3] = 0, but left neighbor flowerbed[2] = 1 (just planted). Cannot plant here.

Step 6: Result - count >= n, return true

1[0]0[1]1[2]0[3]1[4]count = 1

We planted 1 flower (count = 1 >= n = 1). Return true. The greedy scan placed a flower as early as possible.

The scanner checks each position. Index 0 is already planted, index 1 has a planted left neighbor, index 2 passes all three checks so we plant there, index 3 now has a planted left neighbor (the flower we just placed), and index 4 is already planted. We placed 1 flower, which meets our goal of n = 1.

Complexity analysis

MetricValue
TimeO(n), single pass through the flowerbed
SpaceO(1), we modify the array in place and use only a few variables

The early return optimization means we might not even scan the entire array, but worst case we visit every element exactly once.

Edge cases to watch for

  • All empty flowerbed = [0, 0, 0], n = 2: you can plant at index 0 and index 2. Return true.
  • All planted flowerbed = [1, 0, 1, 0, 1], n = 1: no valid spots. Return false.
  • Single plot empty flowerbed = [0], n = 1: boundary conditions on both sides are satisfied. Plant at index 0. Return true.
  • n is zero flowerbed = [1, 0, 1], n = 0: you do not need to plant anything. Return true immediately.
  • Already enough space flowerbed = [0, 0, 0, 0, 0], n = 3: plant at indices 0, 2, and 4. Return true.
  • Exact fit flowerbed = [1, 0, 0, 0, 0, 1], n = 1: only index 2 or 3 works. The greedy scan picks index 2. Return true.

The greedy approach handles all of these without any special-case logic.

The building blocks

This problem is built on two reusable patterns that appear across many LeetCode problems.

1. Greedy local placement

When you can make a locally optimal decision that does not hurt future choices, do it immediately. Here, planting as early as possible is always safe because a flower at position i only blocks positions i - 1 and i + 1. It never reduces the number of valid positions further to the right compared to skipping i.

2. In-place array modification as state tracking

By setting flowerbed[i] = 1 when you plant, you turn the input array into your state tracker. The next iteration naturally sees the updated state and respects the new adjacency constraint. This avoids needing a separate visited set or boolean array.

if left_empty and right_empty:
    flowerbed[i] = 1
    count += 1

This in-place modification pattern is common in problems where you need to mark positions as used or occupied during a single pass.

From understanding to recall

You have read through the greedy scan and it makes sense. Check three conditions, plant if valid, move on. But can you write it from scratch under time pressure?

The details trip people up: remembering to check both boundary conditions separately, modifying the array in place so future checks see the planted flower, and including the early return for efficiency. These are small things, but they matter when you are writing code on a whiteboard or in a timed interview.

Spaced repetition helps you move from "I understand this" to "I can write this cold." You practice the solution at increasing intervals until the boundary checks and the greedy logic are automatic. After a few rounds, you see "greedy placement with adjacency constraints" and the code flows out without hesitation.

Related posts