Can Place Flowers - Problem

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

example_1.py โ€” Basic Case
$ Input: flowerbed = [1,0,0,0,1], n = 1
โ€บ Output: true
๐Ÿ’ก Note: We can plant 1 flower at index 2. The position is safe because both neighbors (index 1 and 3) are empty.
example_2.py โ€” Cannot Plant
$ Input: flowerbed = [1,0,0,0,1], n = 2
โ€บ Output: false
๐Ÿ’ก Note: We can only plant 1 flower at index 2. After planting there, no other positions are safe due to adjacency constraints.
example_3.py โ€” Edge Case
$ Input: flowerbed = [0], n = 1
โ€บ Output: true
๐Ÿ’ก Note: Single empty plot can accommodate one flower since there are no adjacent positions to worry about.

Constraints

  • 1 โ‰ค flowerbed.length โ‰ค 2 ร— 104
  • flowerbed[i] is 0 or 1
  • There are no two adjacent flowers in flowerbed
  • 0 โ‰ค n โ‰ค flowerbed.length

Visualization

Tap to expand
๐ŸŒป Can Place Flowers - Greedy AlgorithmInput: [1, 0, 0, 0, 1], n = 1๐ŸŒน000๐ŸŒนStep 1: Check position i=10Left neighbor: ๐ŸŒน (occupied) โ†’ Cannot plantStep 2: Check position i=2๐ŸŒปLeft: empty โœ“, Right: empty โœ“ โ†’ Plant flower!Planted count: 1, Target: 1 โ†’ SUCCESS!Algorithm Steps:1Iterate through each position2Check if current position is empty3Verify both neighbors are empty4If safe, plant flower immediately5Count planted flowers๐ŸŽฏ Greedy Strategy Key InsightPlanting a flower as early as possible never hurts our ability to plant future flowers!
Understanding the Visualization
1
Survey the Garden
Start from the leftmost plot and examine each position
2
Check Neighbors
For empty plots, verify both left and right neighbors are safe
3
Plant Greedily
If position is safe, plant immediately - don't wait!
4
Continue Forward
Move to next position and repeat until done
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because planting flowers from left to right, whenever possible, maximizes our planting opportunities without any negative consequences for future decisions.
Asked in
LinkedIn 15 Amazon 12 Google 8 Meta 6
125.0K Views
Medium Frequency
~12 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen