#605
Easy Algorithms Can place flowers
Array Greedy
29.1% acceptance
Feb 20, 2026
7350
1317
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn can_place_flowers(mut flowerbed: Vec<i32>, n: i32) -> bool {
let len = flowerbed.len();
let mut count = 0;
for i in 0..len {
if flowerbed[i] == 0 {
let left = i == 0 || flowerbed[i - 1] == 0;
let right = i == len - 1 || flowerbed[i + 1] == 0;
if left && right {
flowerbed[i] = 1;
count += 1;
}
}
}
count >= n
}
}