public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 1;
int result = 0;
for(int i=0; i<flowerbed.length; i++) {
if(flowerbed[i] == 0) {
count++;
}else {
result += (count1)/2;
count = 0;
}
}
if(count != 0) result += count/2;
return result>=n;
}
[Java] Very easy solution

From left to right,
record the index of last1
, iff[i]
is0
, check if this position can be put a1
.
How can we check? From this example, we can know only the0
be center of three0
.
[1,0,0,0,1]
public boolean canPlaceFlowers(int[] f, int m) { int n = f.length; int p1 = 2; for(int i = 0; i < n; i++){ if(f[i] == 1) p1 = i; else { if(i  p1 == 2 && (i + 1 == n  f[i + 1] != 1)){ m; p1 = i; } } } return m <= 0; }

@DNfish even I used the same approach. But, instead of using
result += (count1)/2;
I am directly using
n = (count1)/2; return n < 1;
Not much difference of course :)