Imagine you're a gardener tasked with planting flowers in a long flowerbed! You have a flowerbed represented as an integer array where 0 means an empty plot and 1 means a plot already has a flower planted.
Here's the catch: flowers cannot be planted in adjacent plots - they need space to grow! Your goal is to determine if you can plant n new flowers without violating this no-adjacent-flowers rule.
Input: An integer array flowerbed containing 0's and 1's, and an integer n representing the number of flowers you want to plant.
Output: Return true if you can successfully plant all n flowers, false otherwise.
Example: Given flowerbed = [1,0,0,0,1] and n = 1, you can plant one flower in the middle position (index 2) since it's not adjacent to any existing flowers.
Input & Output
Visualization
Time & Space Complexity
Single pass through the flowerbed array of length n
Only using a few variables regardless of input size
Constraints
- 1 โค flowerbed.length โค 2 ร 104
- flowerbed[i] is 0 or 1
- There are no two adjacent flowers in flowerbed
- 0 โค n โค flowerbed.length