0605. Can Place Flowers
Description
**Input:** flowerbed = [1,0,0,0,1], n = 1
**Output:** true**Input:** flowerbed = [1,0,0,0,1], n = 2
**Output:** falseac
Last updated
**Input:** flowerbed = [1,0,0,0,1], n = 1
**Output:** true**Input:** flowerbed = [1,0,0,0,1], n = 2
**Output:** falseLast updated
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
// edge cases
if (n > (flowerbed.length + 1) / 2) return false;
// plant
for (int i = 0; i < flowerbed.length; i++) {
if (n == 0) return true;
if (flowerbed[i] == 1) continue;
int prev = i == 0 ? 0 : flowerbed[i-1];
int next = i == flowerbed.length - 1 ? 0 : flowerbed[i+1];
if (prev == 0 && next == 0) {
flowerbed[i] = 1;
n--;
}
}
return n == 0;
}
}
/*
lots of disgusting edge cases
*/