Can Place Flowers
Easy
Topics
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
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.Input: hand = [1,0,0,0,1], k = 1 Output: true Explanation: Plant in the middle plot. Its neighbours are both empty.
Example 2
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
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
- Walk the positions from left to right.
- At position
i, treat it as plantable whenhand[i] == 0, the left neighbour is empty or does not exist, and the right neighbour is empty or does not exist. - On planting, set
hand[i] = 1and increment the count. - Return
truethe moment the count reachesk. - If the scan finishes first, return whether the count reached
k— which also handlesk = 0correctly.
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
falsefork = 0. Nothing needs planting, so the answer istrue. - Computing the capacity of a gap with a formula and getting the boundary cases wrong. A gap of length
gbetween two flowers holds(g - 1) / 2, but a gap at the end of the bed holdsg / 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 >= kimport java.util.*;
class Solution {
public boolean canPlaceFlowers(int[] hand, int k) {
int count = 0;
for (int i = 0; i < hand.length; i++) {
if (hand[i] != 0) continue;
boolean leftFree = i == 0 || hand[i - 1] == 0;
boolean rightFree = i == hand.length - 1 || hand[i + 1] == 0;
if (leftFree && rightFree) {
hand[i] = 1;
count++;
if (count >= k) return true;
}
}
return count >= k;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: bool canPlaceFlowers(vector<int>& hand, int k) {
int count = 0, n = (int)hand.size();
for (int i = 0; i < n; i++) {
if (hand[i] != 0) continue;
bool leftFree = (i == 0) || hand[i - 1] == 0;
bool rightFree = (i == n - 1) || hand[i + 1] == 0;
if (leftFree && rightFree) {
hand[i] = 1;
count++;
if (count >= k) return true;
}
}
return count >= k;
}
};/**
* @param {number[]} hand
* @param {number} k
* @return {boolean}
*/ var canPlaceFlowers = function(hand, k) {
let count = 0;
for (let i = 0; i < hand.length; i++) {
if (hand[i] !== 0) continue;
const leftFree = i === 0 || hand[i - 1] === 0;
const rightFree = i === hand.length - 1 || hand[i + 1] === 0;
if (leftFree && rightFree) {
hand[i] = 1;
count++;
if (count >= k) return true;
}
}
return count >= k;
};