605. Can Place Flowers - Detailed Explanation
Problem Statement
Description:
You are given a flowerbed represented as an integer array flowerbed
containing 0's and 1's, where 0 means an empty plot and 1 means a plot already has a flower planted. Flowers cannot be planted in adjacent plots. Given an integer n
, determine if n
new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
Example 1:
- Input:
- flowerbed =
[1, 0, 0, 0, 1]
- n =
1
- flowerbed =
- Output:
true
- Explanation:
You can plant a flower in the middle plot (index 2) to get[1, 0, 1, 0, 1]
.
Example 2:
- Input:
- flowerbed =
[1, 0, 0, 0, 1]
- n =
2
- flowerbed =
- Output:
false
- Explanation:
There is only one spot available to plant a new flower without violating the adjacent rule.
Example 3:
- Input:
- flowerbed =
[0, 0, 1, 0, 0]
- n =
2
- flowerbed =
- Output:
true
- Explanation:
One valid planting is to place flowers at indices 0 and 3 (resulting in[1, 0, 1, 1, 0]
), but note that careful checking of adjacent plots is required to ensure no two planted flowers become adjacent.
Constraints:
- 1 ≤ flowerbed.length ≤ 2 * 10⁴
- flowerbed[i] is 0 or 1
- There are no two adjacent flowers in flowerbed initially
- 0 ≤ n ≤ flowerbed.length
Hints
-
Hint 1:
You need to iterate through the array and, at each empty plot, check if its adjacent plots (or boundaries) are empty. -
Hint 2:
Remember to treat the boundaries carefully: if the plot is at the start or end of the array, you only need to check one neighbor. -
Hint 3:
When you plant a flower, mark that plot as planted (set it to 1) and skip the next plot since it cannot be used to plant another flower.
Approaches
Approach 1: Greedy Iteration
- Idea:
Traverse the flowerbed and check for each plot if it is empty and its neighbors (if they exist) are also empty. If yes, plant a flower by setting that plot to 1 and decrementn
. Continue until you finish scanning orn
becomes 0. - Details:
- For index
i
, check ifflowerbed[i] == 0
. - For the left neighbor: if
i == 0
orflowerbed[i-1] == 0
. - For the right neighbor: if
i == flowerbed.length - 1
orflowerbed[i+1] == 0
. - If both conditions are true, plant a flower (
flowerbed[i] = 1
) and skip the next index.
- For index
- Benefits:
This method is straightforward and runs in O(n) time with O(1) extra space.
Approach 2: Simulation with In-Place Updates
- Idea:
Use a similar method to the greedy approach but simulate the planting process more explicitly. Update the array in-place and count the planted flowers. - Benefits:
This method is effectively the same as the greedy approach and uses constant extra space.
Code Implementations
Python Code
Java Code
Complexity Analysis
-
Time Complexity:
The algorithm scans the flowerbed once, so the time complexity is O(n), where n is the length of the flowerbed. -
Space Complexity:
The solution uses a constant amount of extra space, O(1).
Step-by-Step Walkthrough
-
Initialize a Counter:
- Start with
count = 0
to count the number of flowers planted.
- Start with
-
Iterate Through the Flowerbed:
- For each index
i
, check ifflowerbed[i]
is 0 (i.e., an empty plot).
- For each index
-
Check Neighbors:
- For the left neighbor, if
i
is 0 orflowerbed[i-1]
is 0. - For the right neighbor, if
i
is the last index orflowerbed[i+1]
is 0.
- For the left neighbor, if
-
Plant a Flower:
- If both neighboring checks pass, set
flowerbed[i]
to 1 (simulate planting a flower). - Increment the
count
of planted flowers. - If
count
reachesn
, returntrue
.
- If both neighboring checks pass, set
-
Finish the Loop:
- After iterating through the array, if
count
is at leastn
, returntrue
; otherwise, returnfalse
.
- After iterating through the array, if
Visual Example
Consider the flowerbed [1, 0, 0, 0, 1]
with n = 1
:
-
Index 0:
Value is 1, so skip. -
Index 1:
Value is 0.- Left neighbor (index 0) is 1 → cannot plant here.
-
Index 2:
Value is 0.- Left neighbor (index 1) is 0 and right neighbor (index 3) is 0 → plant a flower here.
- Set
flowerbed[2] = 1
and increment count to 1.
-
Since count equals n, return
true
.
The updated flowerbed becomes [1, 0, 1, 0, 1]
.
Common Mistakes
- Ignoring Boundaries:
Failing to check if the current index is at the start or end of the array may cause an index out-of-bound error. - Not Updating the Array:
Forgetting to mark the plot as planted (setting it to 1) after planting a flower may lead to an incorrect count. - Overplanting:
Not skipping correctly after planting may result in adjacent flowers, which violates the rules.
Edge Cases
- No Available Spots:
When the flowerbed is full or nearly full, the algorithm should correctly returnfalse
. - n Equals 0:
If no new flowers need to be planted, the function should immediately returntrue
. - Single Plot Flowerbed:
Handle cases where the flowerbed contains only one plot.
Alternative Variations and Related Problems
- Modified Constraints:
Variants might involve different rules for adjacent planting or multiple types of flowers. - Other Greedy Problems:
Similar greedy techniques can be applied to interval scheduling, seating arrangements, or resource allocation problems.
Related Problems for Further Practice
- Gas Station
- Jump Game
- Container With Most Water
- Merge Intervals
GET YOUR FREE
Coding Questions Catalog
