You are managing a garden plot represented as an array where each position either contains a planted flower (1) or is empty (0). The garden has a strict rule: no two flowers can be planted in adjacent positions.
Given the current state of the garden and a number of new flowers you want to plant, determine whether it's possible to plant all the new flowers while respecting the adjacency rule.
true if you can plant n new flowers without violating the adjacency constraintfalse if it's impossible to plant all n flowersExample 1:
Input: flowerbed = [1, 0, 0, 0, 1], n = 1 Output: true Explanation: We can plant one flower at index 2. The neighbors at indices 1 and 3 are both empty (0).
Example 2:
Input: flowerbed = [1, 0, 0, 0, 1], n = 2 Output: false Explanation: Only one flower can be planted (at index 2). We cannot plant 2 flowers without creating adjacent plants.
Example 3:
Input: flowerbed = [0, 0, 0], n = 2 Output: true Explanation: We can plant flowers at index 0 and index 2, leaving index 1 empty as a buffer.
Example 4:
Input: flowerbed = [0], n = 1 Output: true Explanation: A single empty plot can accommodate one flower since it has no neighbors.
Hint 1: Greedy Strategy A greedy approach works well here. Try to plant flowers as early as possible in the array whenever you find a valid spot. This maximizes the number of flowers you can plant overall.
Hint 2: Valid Planting Conditions A position
iis valid for planting if:
flowerbed[i]is currently 0 (empty)- The left neighbor (if it exists) is 0
- The right neighbor (if it exists) is 0
Be careful with edge cases at the boundaries of the array!
Hint 3: Early Termination You don't need to scan the entire array. Once you've successfully planted
nflowers, you can returntrueimmediately.
Full Solution `` Approach:
This solution uses a greedy algorithm that scans through the flowerbed from left to right. At each empty position, it checks whether both neighbors (if they exist) are also empty. If so, it plants a flower and increments the counter.
Key Insights:
- Greedy works: Planting as early as possible never hurts our chances of planting more flowers later
- Edge handling: Positions at index 0 and the last index only have one neighbor to check
- Optimization: We can return early once we've planted enough flowers
Time Complexity: O(n) where n is the length of the flowerbed array. We make a single pass through the array.
Space Complexity: O(1) as we only use a constant amount of extra space (we modify the input array in place, but this doesn't count toward space complexity).
def canPlaceFlowers(flowerbed, n):
# Track how many flowers we've successfully planted
planted = 0
length = len(flowerbed)
# Iterate through each position in the flowerbed
for i in range(length):
# Check if current position is empty
if flowerbed[i] == 0:
# Check left neighbor (or assume empty if we're at left edge)
left_empty = (i == 0) or (flowerbed[i - 1] == 0)
# Check right neighbor (or assume empty if we're at right edge)
right_empty = (i == length - 1) or (flowerbed[i + 1] == 0)
# If both neighbors are empty, we can plant here
if left_empty and right_empty:
flowerbed[i] = 1 # Plant the flower
planted += 1
# Early exit if we've planted enough flowers
if planted >= n:
return True
# Check if we managed to plant the required number of flowers
return planted >= n