↓Skip to main content
  1. LeetCode/

LeetCode 605: Can Place Flowers

·2 mins· ·
LeetCode Easy Array Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-28

  • Submissions: Runtime Error ×2 → Wrong Answer → Internal Error → Wrong Answer → Accepted → Wrong Answer ×5 → Accepted

Approach
#

Greedy (Greedy): scanning left to right, plant whenever the plot is 0 and neither neighbor is 1 (treating outside the ends as empty), and set it to 1 so the next plot is blocked. Planting as early as possible never hurts, since a flower further left leaves more room on the right. Finally check that the count is >= n.

My Attempts
#

I solved it in 2022 and again in 2025, failing 10 times in total:

  • 2022: I first counted “empty plots not next to any flower”, but newly planted flowers block their neighbors too, so adjacent empty plots were both counted: [0, 0], n = 2 returned True instead of False. Padding with zeros and marking each planted plot passed.
  • 2025: tracking prev_plant and next_plant, I first typed pre_plant, so the left neighbor always looked empty; after fixing that, the last line read plots <= n, the comparison reversed.

Solution
#

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        bed = [0] + flowerbed + [0]
        count = 0
        for i in range(1, len(bed) - 1):
            if bed[i - 1] == bed[i] == bed[i + 1] == 0:
                bed[i] = 1
                count += 1
        return count >= n

Complexity
#

Time O(n), space O(n) for the sentinels; checking the bounds in place gives O(1).

Mistakes to Avoid
#

  • Update the array after planting, or two adjacent empty plots both count.
  • The comparison is count >= n.

Interview Explanation
#

With an empty sentinel at each end I scan left to right, planting and marking any plot with empty neighbors, since planting early never hurts, then check the count is at least n. Time O(n).

Related

LeetCode 561: Array Partition
·1 min
LeetCode Easy Array Greedy Sorting Counting-Sort
LeetCode 561 note: sort and pair neighbors; each pair’s minimum is the element at an even index, so sum those
LeetCode 455: Assign Cookies
·2 mins
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 note: sort children and cookies, satisfying the least greedy child with the smallest cookie that suffices; each pointer walks once
LeetCode 598: Range Addition II
·2 mins
LeetCode Easy Array Math
LeetCode 598 note: every operation covers the top-left corner, so the most-incremented cells form the intersection of all operations: min(a) × min(b)
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 566: Reshape the Matrix
·2 mins
LeetCode Easy Array Matrix Simulation
LeetCode 566 note: return the original if the element counts differ; otherwise element k goes to (k // c, k % c)
LeetCode 575: Distribute Candies
·1 min
LeetCode Easy Array Hash-Table
LeetCode 575 note: at most n / 2 candies can be eaten and at most len(set) kinds exist; the answer is the smaller of the two