You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array flowerbed
containing 0
's and 1
's, where 0
means empty and 1
means not empty, and an integer n
, return true
if n
new flowers can be planted in the flowerbed
without violating the no-adjacent-flowers rule and false
otherwise.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Constraints:
1 <= flowerbed.length <= 2 * 10^4
flowerbed[i]
is 0
or 1
.flowerbed
.0 <= n <= flowerbed.length
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy involves checking every single possible location in the garden to see if we can plant a flower. We'll try planting a flower in each available spot, one by one, and see if it works.
Here's how the algorithm would work step-by-step:
def can_place_flowers_brute_force(flowerbed, number_of_flowers_to_plant):
garden_length = len(flowerbed)
flowers_planted = 0
for i in range(garden_length):
# Check if the current spot is empty
if flowerbed[i] == 0:
can_plant = True
# Check left adjacent spot
if i > 0 and flowerbed[i - 1] == 1:
can_plant = False
# Check right adjacent spot
if i < garden_length - 1 and flowerbed[i + 1] == 1:
can_plant = False
if can_plant:
# Plant a flower if possible
flowerbed[i] = 1
#Reduce the amount of flowers to plant
flowers_planted += 1
if flowers_planted >= number_of_flowers_to_plant:
return True
return flowers_planted >= number_of_flowers_to_plant
The goal is to plant as many flowers as possible in designated spots, but flowers can't be planted in adjacent spots. The optimal strategy involves checking each spot and planting a flower only if it's possible without violating the adjacency rule.
Here's how the algorithm would work step-by-step:
def canPlaceFlowers(flowerbed, number_of_flowers):
number_of_plots = len(flowerbed)
flowers_planted = 0
for i in range(number_of_plots):
# Check if current spot is empty
if flowerbed[i] == 0:
empty_left_plot = (i == 0) or (flowerbed[i - 1] == 0)
empty_right_plot = (i == number_of_plots - 1) or (flowerbed[i + 1] == 0)
# If left and right spots are empty, plant a flower
if empty_left_plot and empty_right_plot:
flowerbed[i] = 1
# Increment planted flowers count.
flowers_planted += 1
if flowers_planted >= number_of_flowers:
return True
# Check if planted flowers equals required flowers
return flowers_planted >= number_of_flowers
Case | How to Handle |
---|---|
flowerbed is null or empty | If flowerbed is null or empty, return true if n is 0, otherwise return false. |
flowerbed has length 1 | If the flowerbed has length 1, check if the single plot is 0, if so increment planted flowers, and return if planted flowers >= n. |
n is 0 | If n is 0, we can always plant 0 flowers, so return true. |
flowerbed contains only 1s | If the flowerbed contains only 1s, we cannot plant any flowers, so return false if n > 0. |
flowerbed contains only 0s | If the flowerbed contains only 0s, we can plant (length + 1) / 2 flowers (integer division). |
n is greater than the maximum possible number of plantable flowers | The algorithm should correctly identify when it's impossible to plant enough flowers and return false. |
flowerbed starts and/or ends with 0s | The algorithm should correctly handle planting flowers at the beginning and end of the array. |
Large flowerbed size | The solution should scale linearly with the size of the flowerbed to avoid time limit exceeded errors. |