Can Place Flowers

Easy

Topics
ArrayGreedy

hand is a flowerbed where 0 is an empty plot and 1 holds a flower. Flowers cannot be planted in adjacent plots, and the given bed already respects that.

Return true if k more flowers can be planted without ever putting two flowers side by side.

LeetCode names the arguments flowerbed and n. The harness here supplies them as hand and k: hand is the flowerbed and k is how many flowers you must fit.

Example 1

1001020314
flower already therea plot that can take one
This bed of 5 plots has room for 1 more flower, and k = 1 is asked for — so the answer is true. A plot is usable when it is empty and neither neighbour holds a flower, and a plot at either end of the bed has only one neighbour to satisfy. That end rule is what makes a bed like [0] able to take a flower at all.
The bed given already obeys the no-adjacent rule; you are only adding to it.
Input:  hand = [1,0,0,0,1], k = 1
Output: true
Explanation: Plant in the middle plot. Its neighbours are both empty.

Example 2

1001020314
flower already therea plot that can take one
Input:  hand = [1,0,0,0,1], k = 2
Output: false
Explanation: Only one plot in that gap is usable, so two will not fit.

Example 3

0001120304
flower already therea plot that can take one
This bed of 5 plots has room for 2 more flowers, and k = 2 is asked for — so the answer is true.
Input:  hand = [0,0,1,0,0], k = 2
Output: true
Explanation: Plant at both ends. An end plot has only one neighbour to check.

Constraints

  • 1 <= hand.length <= 2 * 10^4
  • hand[i] is 0 or 1
  • There are no two adjacent flowers in hand
  • 0 <= k <= hand.length
Editorial — intuition, approach, complexity and pitfalls

Intuition

Scan left to right and plant in the first plot you legally can. This is safe, and the reason is worth stating: planting as far left as possible never blocks an option that planting later would have kept open. Moving a flower rightwards can only reduce the room left behind it.

A plot is legal when it is empty and both neighbours are empty, with a missing neighbour — at either end of the bed — counting as empty. That edge case is most of the difficulty: [0] with k = 1 is true, and a solution that insists on two real neighbours gets it wrong.

Once you plant, mark the plot as occupied so the next plot is correctly rejected. Mutating the input is the simplest way; if that is not acceptable, skip forward two positions instead, which has the same effect.

Stop as soon as the count reaches k. Continuing is harmless but pointless, and k = 0 should return true without planting anything.

Approach

  1. Walk the positions from left to right.
  2. At position i, treat it as plantable when hand[i] == 0, the left neighbour is empty or does not exist, and the right neighbour is empty or does not exist.
  3. On planting, set hand[i] = 1 and increment the count.
  4. Return true the moment the count reaches k.
  5. If the scan finishes first, return whether the count reached k — which also handles k = 0 correctly.

Walkthrough

hand = [1,0,0,0,1], k = 1

   i=0  occupied
   i=1  empty, but left neighbour hand[0] = 1  -> no
   i=2  empty, hand[1] = 0, hand[3] = 0        -> PLANT, count 1
        count reached k, answer true

hand = [0,0,1,0,0], k = 2

   i=0  empty, no left neighbour, hand[1] = 0  -> PLANT, count 1
        bed is now [1,0,1,0,0]
   i=1  hand[0] is now 1                       -> no
   i=2  occupied
   i=3  hand[2] = 1                            -> no
   i=4  empty, hand[3] = 0, no right neighbour  -> PLANT, count 2
        answer true

   the ends are what this case tests: requiring two real
   neighbours would reject both plots and answer false.

Complexity

  • Time — O(n): one pass over the bed, with a constant check per plot.
  • Space — O(1): a counter; the bed is modified in place.

Common Pitfalls

  • Requiring both neighbours to exist, which wrongly rejects the first and last plots.
  • Not marking the plot after planting, so two adjacent plots both look legal.
  • Returning false for k = 0. Nothing needs planting, so the answer is true.
  • Computing the capacity of a gap with a formula and getting the boundary cases wrong. A gap of length g between two flowers holds (g - 1) / 2, but a gap at the end of the bed holds g / 2 — scanning avoids having to remember which is which.
  • Reading the neighbour before checking that the index is in range.

Full Solution — all languages

class Solution:
    def canPlaceFlowers(self, hand, k):
        count = 0

        for i in range(len(hand)):
            if hand[i] != 0:
                continue

            # A missing neighbour, at either end of the bed, counts as empty.
            left_free = i == 0 or hand[i - 1] == 0
            right_free = i == len(hand) - 1 or hand[i + 1] == 0

            if left_free and right_free:
                hand[i] = 1          # so position i + 1 is correctly rejected
                count += 1
                if count >= k:
                    return True

        return count >= k
SensAI
Beta
Tap the mic to stop voice mode