-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowerPlanting.java
More file actions
29 lines (25 loc) · 882 Bytes
/
Copy pathFlowerPlanting.java
File metadata and controls
29 lines (25 loc) · 882 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
package LeetcodePractice;
public class FlowerPlanting {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
int len = flowerbed.length;
for (int i = 0; i < len; i++) {
if (flowerbed[i] == 0) {
boolean emptyLeft = (i == 0) || (flowerbed[i - 1] == 0);
boolean emptyRight = (i == len - 1) || (flowerbed[i + 1] == 0);
if (emptyLeft && emptyRight) {
flowerbed[i] = 1;
count++;
if (count >= n) return true;
}
}
}
return count >= n;
}
public static void main(String[] args) {
FlowerPlanting obj = new FlowerPlanting();
int[] flowerbed = {1, 0, 0, 0, 1};
int n = 1;
System.out.println(obj.canPlaceFlowers(flowerbed, n));
}
}