605. Can Place Flowers
#array
Intuition
Approach
Complexity
Space Complexity
Time Complexity
Code
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if(n == 0) return true;
for(int i = 0; i < flowerbed.length; ++i) {
if(flowerbed[i] == 0) {
// check adjacent plots
if( ( i == 0 || flowerbed[i-1] == 0) && (i == flowerbed.length-1 || flowerbed[i+1] == 0)) {
// put flower in the plot and skip the adjacent plot
i++;
// decrease remaining plants
n--;
// if none yes all planted
if(n == 0) return true;
}
}
}
return false;
}
}Last updated