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 * 104flowerbed[i] is 0 or 1.flowerbed.0 <= n <= flowerbed.lengthProblem Overview: You are given a binary array flowerbed where 1 means a flower is already planted and 0 means empty. You need to determine whether you can plant n new flowers without violating the rule that no two flowers can be adjacent.
Approach 1: Counting Possible Placements (O(n) time, O(1) space)
This method analyzes stretches of consecutive empty plots and calculates how many flowers can fit inside each segment. If you find a block of k zeros surrounded by planted flowers, the maximum placements inside that segment follow a predictable pattern: you can place (k-1)/2 flowers. Edge segments (start or end of the array) allow slightly more placements because only one side has a restriction. The algorithm scans the array once, counts zero segments, and accumulates the total possible placements. If the sum reaches or exceeds n, planting is feasible. The logic is purely arithmetic on segments, which keeps the time complexity O(n) and space complexity O(1). This technique works well when reasoning about contiguous gaps in an array.
Approach 2: Greedy Planting by Scanning (O(n) time, O(1) space)
The greedy approach directly simulates planting while scanning the array from left to right. For each index i, check three conditions: the current plot is empty, the left neighbor is empty (or doesn't exist), and the right neighbor is empty (or doesn't exist). If all conditions hold, plant a flower by setting flowerbed[i] = 1 and decrease n. This works because placing a flower as early as possible never blocks a better future placement. The algorithm only performs constant-time checks per position, so the total runtime is O(n) with O(1) extra space. This is a classic example of a greedy decision: a locally optimal placement leads to a globally optimal result.
During the scan you simply iterate once through the array, perform neighbor checks, and update the state when planting occurs. The modification is safe because once a flower is placed, the next index automatically becomes invalid for planting, which the neighbor checks naturally enforce.
Recommended for interviews: The greedy scanning approach is what interviewers typically expect. It shows you can reason about constraints directly and implement a linear pass with constant space. The counting approach demonstrates deeper pattern recognition with segments and boundaries, which can help when generalizing similar gap counting problems in array processing.
This approach involves a single pass through the flowerbed array, considering each plot one by one. For each zero found, we check whether it's possible to plant a flower by ensuring that both neighboring plots (if they exist) are not planted. If it's possible, we plant the flower (i.e., change the zero to one) and reduce the count of n. The process stops early if n becomes zero. This greedy method ensures efficiency by trying to plant a flower at each opportunity.
This C solution iterates through the flowerbed, checking the conditions for planting flowers by ensuring adjacent plots are empty. It directly modifies the flowerbed array in the process, which simulates planting the flowers. If the required number of flowers (n) is planted, it returns early.
Time Complexity: O(m), where m is the length of the flowerbed array.
Space Complexity: O(1), as no extra space is used except for variables.
This approach calculates the maximum number of flowers that can be planted by counting the stretches of zeros between planted flowers or array boundaries. For each encounter of a continuous stretch of zeros, determine how many flowers can be planted and accumulate this count until it reaches or exceeds n, or if it proves impossible.
This C solution leverages a counting mechanism, iterating through the flowerbed and adding cushions (zeros) at the boundaries to simplify calculations. The number of available spots for planting is incrementally calculated and checked against n.
Time Complexity: O(m), where m is the length of the flowerbed.
Space Complexity: O(1), only a constant amount of extra space is required.
We directly traverse the array flowerbed. For each position i, if flowerbed[i]=0 and its adjacent positions on the left and right are also 0, then we can plant a flower at this position. Otherwise, we cannot. Finally, we count the number of flowers that can be planted. If it is not less than n, we return true, otherwise we return false.
The time complexity is O(n), where n is the length of the array flowerbed. We only need to traverse the array once. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Greedy Planting by Scanning | Time Complexity: O(m), where m is the length of the flowerbed array. |
| Counting Possible Placements | Time Complexity: O(m), where m is the length of the flowerbed. |
| Greedy | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counting Possible Placements | O(n) | O(1) | When analyzing zero segments or reasoning mathematically about placement capacity |
| Greedy Planting by Scanning | O(n) | O(1) | General case and most common interview solution using greedy checks |
Can Place Flowers - Leetcode 605 - Python • NeetCode • 64,362 views views
Watch 9 more video solutions →Practice Can Place Flowers with our built-in code editor and test cases.
Practice on FleetCode