-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
56 lines (48 loc) · 1.26 KB
/
Solution.java
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
47
48
49
50
51
52
53
54
55
56
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if (n == 0) {
return true;
}
int m = flowerbed.length;
List<Integer> data = new ArrayList<Integer>(m + 2);
data.add(0);
data.addAll(Arrays.stream(flowerbed).boxed().toList());
data.add(0);
for (int i = 1; i <= m; ++i) {
if (data.get(i - 1) == 1 || data.get(i) == 1 || data.get(i + 1) == 1) {
continue;
}
data.set(i, 1);
--n;
if (n == 0) {
return true;
}
}
return false;
}
}
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if (n == 0) {
return true;
}
int m = flowerbed.length;
for (int i = 0; i < m; ++i) {
if (i - 1 >= 0 && flowerbed[i - 1] == 1) {
continue;
}
if (flowerbed[i] == 1) {
continue;
}
if (i + 1 < m && flowerbed[i + 1] == 1) {
continue;
}
flowerbed[i] = 1;
--n;
if (n == 0) {
return true;
}
}
return false;
}
}