-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. 605. Can Place Flowers
46 lines (39 loc) · 1.08 KB
/
4. 605. 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
38
39
40
41
42
43
44
45
46
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
boolean flag = false;
int cnt = 0;
int len= flowerbed.length;
//for check
for(int i=0;i<len ; i++){
if(flowerbed[i]==0){
//left most
if(i==0){
//only one element
//greater than one
if((i==len-1) || (i+1<=len-1 && flowerbed[i+1]==0)){
flowerbed[i]=1;
cnt++;
}
}
//centre
//left
//right
else if((i-1>=0 && flowerbed[i-1]==0) && (i+1<=len-1 && flowerbed[i+1]==0)){
flowerbed[i]=1;
cnt++;
}
//right most
else if(i+1>len-1 && flowerbed[i-1]==0){
flowerbed[i]=1;
cnt++;
}
}
//check for count
if(cnt>=n){
flag=true;
break;
}
}
return flag;
}
}