-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanPlaceFlowers.java
More file actions
131 lines (109 loc) · 3.33 KB
/
CanPlaceFlowers.java
File metadata and controls
131 lines (109 loc) · 3.33 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package Algorithms.IntegerArray;
import java.util.Arrays;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 05 April 2025
*/
public class CanPlaceFlowers {
public static void main(String[] args) {
int[] flowerbed = {1,0,0,0,1};
int n = 1;
System.out.println(canPlaceFlowers(flowerbed, n));
}
public static boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
int len = flowerbed.length;
for (int i = 0; i < len; i++) {
if (flowerbed[i] == 0
&&
(i == 0 || flowerbed[i - 1] == 0) // before i is 0
&&
(i == len - 1 || flowerbed[i + 1] == 0) // after i is 0
) {
flowerbed[i] = 1; // Plant a flower here
count++;
}
}
return count >= n;
}
public boolean canPlaceFlowers2(int[] flowerbed, int n) {
int l=flowerbed.length;
for(int i=0;i<l;i++){
if(flowerbed[i] == 0){
boolean prev = (i==0) || (flowerbed[i-1] == 0);
boolean next = (i == l-1) || (flowerbed[i+1] == 0);
if(prev && next){
flowerbed[i] = 1;
n--;
if(n<=0) return true;
}
}
}
return n<=0;
}
public boolean canPlaceFlowers3(int[] flowerbed, int n) {
int len = flowerbed.length;
int i = 0;
while(i < len && n > 0) {
if(flowerbed[i] == 1) i += 2; // skip the next 2 beds
else if(i == len - 1 || flowerbed[i + 1] == 0) {
n--;
i += 2;
}
else i += 3;
}
return n <= 0;
}
/**
*
* 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0
*
* 1 0 0 0 0 1 0 0 0 0 1
* 3 0s -> 1
* 4 0s -> 1
* 5 0s -> 2
* 6 0s -> 2
* 7 0s -> 3
* 8 0s -> 3
* even (n-1)/2 odd n/2
*
* so, instead of checking edge cases like i=0 && i=len-1, we can add 0s to the start and end of the array.
*/
public boolean canPlaceFlowersMyApproach(int[] flowerbed, int n) {
int[] f = new int[flowerbed.length + 2];
for (int i=0; i<flowerbed.length; i++) f[i+1]=flowerbed[i];
int c=0;
for (int i=0; i<f.length; i++) {
int zeros = 0;
while(i<f.length && f[i]==0){
zeros++;
i++;
}
if((zeros & 1)==1) c += zeros/2;
else c += (zeros-1)/2;
}
return c >= n;
}
/**
NOT WORKING
PATTERNS:
---------
1) 1 flower -> B1 | ADD1 - B3, ADD2 - B5
2) 2 flowers -> B3 | ADD1 - B5, ADD2 - B7
3) 3 flowers -> B5
4) 4 flowers -> B7
5) 5 flowers -> B9
6) 6 flowers -> B11
7) 7 flowers -> B13
8) 8 flowers -> B15
n flowers -> min beds n+2
but it'll fail scenarios like [1,0,1,0,0,1,0] && n=1
*/
public boolean canPlaceFlowersMyOldApproach(int[] flowerbed, int n) {
int len = flowerbed.length;
int ones = (int)Arrays.stream(flowerbed).filter(f->f==1).count() + n;
if (len == 1) return ones<=1;
if (flowerbed[0]==0 && flowerbed[1]==1) return (ones*2-1) <= len-1;
else return (ones*2-1) <= len;
}
}