Array / String· 15 min
Can Place Flowers
LeetCode #605 · Easy · Interactive step-by-step visualization
Setup
Example input
flowerbed = [1, 0, 0, 0, 0, 0, 1], n = 2Step 1 / 14
Current step
1 / 14Need to place 2 flowers without adjacent flowers. Scan from left to right.
Step log
Need 2 flowersPlaced 0 / 2
Flowerbed
0
1
2
3
4
5
6
Active
Comparing
Committed
Rejected or blocked
can-place-flowers.java
1public boolean canPlaceFlowers(int[] bed, int n) {2 int planted = 0;3 for (int i = 0; i < bed.length && planted < n; i++) {4 boolean left = i == 0 || bed[i - 1] == 0;5 boolean right = i == bed.length - 1 || bed[i + 1] == 0;6 if (bed[i] == 0 && left && right) {7 bed[i] = 1; planted++;8 }9 }10 return planted >= n;11}● Java
Time
O(n)
Space
O(1)
Pattern
greedy
Frames
14
