-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCanPlaceFlowers.java
More file actions
31 lines (31 loc) · 836 Bytes
/
CanPlaceFlowers.java
File metadata and controls
31 lines (31 loc) · 836 Bytes
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
/**
* 605. Can Place Flowers
* 贪心
* @author LBW
*/
public class CanPlaceFlowers {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int res = 0, len = flowerbed.length;
if (len < 2) {
res = flowerbed[0] == 0 ? 1 : 0;
return res >= n;
}
//judge 0
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
res += 1;
flowerbed[0] = 1;
}
for (int i = 1; i < len - 1; i++) {
if (flowerbed[i] == 0 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0) {
res += 1;
flowerbed[i] = 1;
}
}
//judge len-1
if(flowerbed[len - 1] == 0 && flowerbed[len - 2] == 0) {
res += 1;
flowerbed[len - 1] = 1;
}
return res >= n;
}
}