-
Notifications
You must be signed in to change notification settings - Fork 0
/
Can place flowers
37 lines (36 loc) · 1.08 KB
/
Can place flowers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int s=flowerbed.length;
int count=0;
if(s==1){
if(flowerbed[0]==1 && n==0) return true;
else if(flowerbed[0]==0 && n>=0) return true;
else return false;
}
for(int i=0; i<flowerbed.length; i++){
if(flowerbed[i]==1) continue;
else {
if(i==0){
if(flowerbed[i]==0 && flowerbed[i+1]==0){
flowerbed[i]=1;
count++;
}
}
else if(i==s-1){
if(flowerbed[i]==0 && flowerbed[i-1]==0){
flowerbed[i]=1;
count++;
}
}
else{
if(flowerbed[i-1]==0 && flowerbed[i+1]==0){
flowerbed[i]=1;
count++;
}
}
}
}
return count>=n;
}
}
#Status: Solved