Can Place Flowers LeetCode

The problem at hand involves determining whether we can plant a certain number of flowers in a flowerbed while adhering to the constraint that no two adjacent plots can be planted. The Can Place Flowers LeetCode problem is relevant not only for gardening enthusiasts but also for optimizing space in various real-world scenarios such as urban planning or resource allocation. It highlights the importance of efficiently utilizing resources while adhering to certain constraints.

Problem statement

Given a long flowerbed, denoted by a list flowerbed, in which some plots are planted (represented by 1's) while others are empty (represented by 0), our goal is to return True if n empty plots in the flowerbed can be planted such that no adjacent plots are planted, and False otherwise.

Sample input

flowerbed = [1,0,0,0,0,0,1]
n = 2

Sample output

True

Constraints:

  • 1 <= flowerbed.length <= 2 * 104

  • flowerbed[i] is 0 or 1.

  • There are no two adjacent flowers in flowerbed.

  • 0 <= n <= flowerbed.length

Knowledge test

Test your understanding of the problem with the quiz below:

Choose the correct answer.

1

Given the flowerbed [0, 1, 0, 0, 0, 1, 0, 0, 0, 1], can we plant 3 flowers without any of them being adjacent to each other?

A)

Yes

B)

No

C)

Cannot be determined

Question 1 of 20 attempted

Algorithm

The intuitive solution is to initialize a counter to 0 and iterate over the flowerbed list. Upon encountering an empty plot, check if the plots preceding and succeeding this plot are empty or not. If they are empty, meaning that the said plot can be planted, increment the value of the counter. At the end of the iteration, if the value of the counter is greater than or equal to n, return True, otherwise, return False.

Initialization
Initialization
1 of 6

Coding example

The Python implementation of the algorithm is as follows:

def canPlaceFlowers(flowerbed, n):
allowedPlants = 0
i = 0
while i < len(flowerbed):
if flowerbed[i] == 1:
i += 2
else:
if i == len(flowerbed)-1:
allowedPlants += 1
break
if flowerbed[i+1] == 0:
allowedPlants += 1
i += 2
else:
i += 1
return allowedPlants >= n
print(canPlaceFlowers([1,0,1,0,1], 1)) # False
print(canPlaceFlowers([1,0,0,0,0,0,1], 2)) # True
print(canPlaceFlowers([0,1,0,1,0,0,1], 1)) # False

Explanation

Here is the step-by-step breakdown of the solution that we implemented:

  • Line 2: We start with initializing a variable called allowedPlants that will serve as our counter.

  • Lines 4–6: Next, we start a while loop to iterate over the flowerbed list. Let’s first handle the case where the encountered plot is already planted. In this case, we can not plant the empty plot next to it since this would violate the no-adjacent-flowers condition. Hence, we skip the plot next to it.

  • Lines 7–15: Now, let’s discuss the situation where the encountered plot is empty. We’ll place two checks here:

    • Lines 8–10: The first condition will check if we have reached the last plot in the flowerbed list. If the condition is true, we increment the value of allowedPlants by 1, indicating that this plot can be planted, and break out of the while loop.

    • Lines 11–15: If the first condition is False, we place a second if-else block to check whether the plot succeeding this plot is empty or not. If it is empty, we'll increment the value of allowedPlants by 2, indicating that this plot can be planted, and also skip the succeeding plot since it can no longer be planted. If the succeeding plot is not empty, we just increment the value of allowedPlants by 1 and move to the succeeding plot.

  • Line 16: Finally, we compare the value of allowedPlants with n. If allowedPlants is bigger than or equal to n, we return True, otherwise, we return False.

Time complexity

The time complexity of the approach is O(n)O(n), where nn represents the length of the flowerbed array. This complexity arises from the while loop that iterates through each element of the flowerbed array once. In the worst-case scenario, every element of the array is visited. Within the loop, the operations involve simple comparisons and additions, which are constant-time operations. As a result, the overall time complexity is determined by the length of the flowerbed array, leading to linear time complexity.

Space complexity

The space complexity of the above implementation is O(1)O(1), meaning it uses a constant amount of space regardless of the input size. The only additional space utilized is for storing variables such as allowedPlants, i, and loop control variables. These variables occupy a fixed amount of space irrespective of the size of the input flowerbed array or other input parameters. Consequently, the space required does not scale with the input size, resulting in constant space complexity.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved