Skip to content

Commit 6719bd9

Browse files
refactor 605
1 parent f6cb034 commit 6719bd9

File tree

2 files changed

+121
-45
lines changed

2 files changed

+121
-45
lines changed

src/main/java/com/fishercoder/solutions/_605.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,57 @@ Given a flowerbed (represented as an array containing 0 and 1, where 0 means emp
2525
*/
2626
public class _605 {
2727

28-
public boolean canPlaceFlowers_more_concise_version(int[] flowerbed, int n) {
29-
int count = 0;
30-
int i = 0;
31-
while (i < flowerbed.length) {
32-
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
33-
count++;
34-
flowerbed[i] = 1;
28+
public static class Solution1 {
29+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
30+
int count = 0;
31+
int i = 0;
32+
while (i < flowerbed.length) {
33+
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
34+
count++;
35+
flowerbed[i] = 1;
36+
}
37+
if (count >= n) {
38+
return true;
39+
}
40+
i++;
3541
}
3642
if (count >= n) {
3743
return true;
3844
}
39-
i++;
40-
}
41-
if (count >= n) {
42-
return true;
45+
return false;
4346
}
44-
return false;
4547
}
4648

47-
public boolean canPlaceFlowers(int[] flowerbed, int n) {
48-
int len = flowerbed.length;
49-
if (len == 1) {
50-
if ((flowerbed[0] == 0 && n <= 1) || n == 0) {
51-
return true;
49+
public static class Solution2 {
50+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
51+
int len = flowerbed.length;
52+
if (len == 1) {
53+
if ((flowerbed[0] == 0 && n <= 1) || n == 0) {
54+
return true;
55+
}
56+
return false;
5257
}
53-
return false;
54-
}
55-
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
56-
flowerbed[0] = 1;
57-
n--;
58-
}
59-
for (int i = 1; i < len - 1; i++) {
60-
if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
58+
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
59+
flowerbed[0] = 1;
60+
n--;
61+
}
62+
for (int i = 1; i < len - 1; i++) {
63+
if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
64+
n--;
65+
flowerbed[i] = 1;//modify the input, discuss this with interviwer, if not allowed, then have a copy of this input and modify copy
66+
}
67+
if (n <= 0) {
68+
return true;
69+
}
70+
}
71+
if (len >= 2 && flowerbed[len - 2] == 0 && flowerbed[len - 1] == 0) {
6172
n--;
62-
flowerbed[i] = 1;//modify the input, discuss this with interviwer, if not allowed, then have a copy of this input and modify copy
6373
}
6474
if (n <= 0) {
6575
return true;
6676
}
77+
return false;
6778
}
68-
if (len >= 2 && flowerbed[len - 2] == 0 && flowerbed[len - 1] == 0) {
69-
n--;
70-
}
71-
if (n <= 0) {
72-
return true;
73-
}
74-
return false;
7579
}
7680

7781
}

src/test/java/com/fishercoder/_605Test.java

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,154 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _605Test {
10-
private static _605 test;
10+
private static _605.Solution1 solution1;
11+
private static _605.Solution2 solution2;
1112
private static int[] flowerbed;
1213
private static int n;
1314

1415
@BeforeClass
1516
public static void setup() {
16-
test = new _605();
17+
solution1 = new _605.Solution1();
18+
solution2 = new _605.Solution2();
1719
}
1820

1921
@Test
2022
public void test1() {
2123
flowerbed = new int[]{1, 0, 0, 0, 1};
2224
n = 1;
23-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
25+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
2426
}
2527

2628
@Test
2729
public void test2() {
2830
flowerbed = new int[]{1, 0, 0, 0, 1};
2931
n = 2;
30-
assertEquals(false, test.canPlaceFlowers(flowerbed, n));
32+
assertEquals(false, solution1.canPlaceFlowers(flowerbed, n));
3133
}
3234

3335
@Test
3436
public void test3() {
3537
flowerbed = new int[]{1, 0, 0, 0, 0, 1};
3638
n = 2;
37-
assertEquals(false, test.canPlaceFlowers(flowerbed, n));
39+
assertEquals(false, solution1.canPlaceFlowers(flowerbed, n));
3840
}
3941

4042
@Test
4143
public void test4() {
4244
flowerbed = new int[]{1, 0, 1, 0, 1, 0, 1};
4345
n = 1;
44-
assertEquals(false, test.canPlaceFlowers(flowerbed, n));
46+
assertEquals(false, solution1.canPlaceFlowers(flowerbed, n));
4547
}
4648

4749
@Test
4850
public void test5() {
4951
flowerbed = new int[]{0, 0, 1, 0, 1};
5052
n = 1;
51-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
53+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
5254
}
5355

5456
@Test
5557
public void test6() {
5658
flowerbed = new int[]{1, 0, 0, 0, 1, 0, 0};
5759
n = 2;
58-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
60+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
5961
}
6062

6163
@Test
6264
public void test7() {
6365
flowerbed = new int[]{0, 0, 1, 0, 0};
6466
n = 2;
65-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
67+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
6668
}
6769

6870
@Test
6971
public void test8() {
7072
flowerbed = new int[]{1};
7173
n = 0;
72-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
74+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
7375
}
7476

7577
@Test
7678
public void test9() {
7779
flowerbed = new int[]{0};
7880
n = 0;
79-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
81+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
8082
}
8183

8284
@Test
8385
public void test10() {
8486
flowerbed = new int[]{0};
8587
n = 1;
86-
assertEquals(true, test.canPlaceFlowers(flowerbed, n));
88+
assertEquals(true, solution1.canPlaceFlowers(flowerbed, n));
89+
}
90+
91+
@Test
92+
public void test11() {
93+
flowerbed = new int[]{1, 0, 0, 0, 1};
94+
n = 1;
95+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
96+
}
97+
98+
@Test
99+
public void test12() {
100+
flowerbed = new int[]{1, 0, 0, 0, 1};
101+
n = 2;
102+
assertEquals(false, solution2.canPlaceFlowers(flowerbed, n));
103+
}
104+
105+
@Test
106+
public void test13() {
107+
flowerbed = new int[]{1, 0, 0, 0, 0, 1};
108+
n = 2;
109+
assertEquals(false, solution2.canPlaceFlowers(flowerbed, n));
110+
}
111+
112+
@Test
113+
public void test14() {
114+
flowerbed = new int[]{1, 0, 1, 0, 1, 0, 1};
115+
n = 1;
116+
assertEquals(false, solution2.canPlaceFlowers(flowerbed, n));
117+
}
118+
119+
@Test
120+
public void test15() {
121+
flowerbed = new int[]{0, 0, 1, 0, 1};
122+
n = 1;
123+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
124+
}
125+
126+
@Test
127+
public void test16() {
128+
flowerbed = new int[]{1, 0, 0, 0, 1, 0, 0};
129+
n = 2;
130+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
131+
}
132+
133+
@Test
134+
public void test17() {
135+
flowerbed = new int[]{0, 0, 1, 0, 0};
136+
n = 2;
137+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
138+
}
139+
140+
@Test
141+
public void test18() {
142+
flowerbed = new int[]{1};
143+
n = 0;
144+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
145+
}
146+
147+
@Test
148+
public void test19() {
149+
flowerbed = new int[]{0};
150+
n = 0;
151+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
152+
}
153+
154+
@Test
155+
public void test20() {
156+
flowerbed = new int[]{0};
157+
n = 1;
158+
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
87159
}
88160
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy