Skip to content

Commit e7015db

Browse files
jinal-sojitraAchal-Hingrajiya
authored andcommitted
CanPlaceFlowers.java added
1 parent b770566 commit e7015db

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
3+
int len=flowerbed.length;
4+
int count = 0;
5+
for(int i = 0; i < len && count < n; i++) {
6+
if(flowerbed[i] == 0) {
7+
int next = (i == flowerbed.length - 1) ? 0 : flowerbed[i + 1];
8+
int prev = (i == 0) ? 0 : flowerbed[i - 1];
9+
if(next == 0 && prev == 0) {
10+
flowerbed[i] = 1;
11+
count++;
12+
}
13+
}
14+
}
15+
16+
return count == n;
17+
}
18+
}

0 commit comments

Comments
 (0)