Skip to content

Commit 13aa0e7

Browse files
optimize 605
1 parent 9458fb2 commit 13aa0e7

2 files changed

Lines changed: 7 additions & 64 deletions

File tree

  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

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

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,62 +27,14 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
2727

2828
public static class Solution2 {
2929
public boolean canPlaceFlowers(int[] flowerbed, int n) {
30-
int len = flowerbed.length;
31-
if (len == 1) {
32-
if ((flowerbed[0] == 0 && n <= 1) || n == 0) {
33-
return true;
34-
}
35-
return false;
36-
}
37-
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
38-
flowerbed[0] = 1;
39-
n--;
40-
}
41-
for (int i = 1; i < len - 1; i++) {
42-
if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
43-
n--;
44-
// modify the input, discuss this with interviwer, if not allowed, then have a
45-
// copy of this input and modify copy
46-
flowerbed[i] = 1;
47-
}
48-
if (n <= 0) {
49-
return true;
50-
}
51-
}
52-
if (len >= 2 && flowerbed[len - 2] == 0 && flowerbed[len - 1] == 0) {
53-
n--;
54-
}
55-
if (n <= 0) {
56-
return true;
57-
}
58-
return false;
59-
}
60-
}
61-
62-
public static class Solution3 {
63-
public boolean canPlaceFlowers(int[] flowerbed, int n) {
64-
if (flowerbed.length == 1) {
65-
if (flowerbed[0] == 0) {
66-
return n <= 1;
67-
} else {
68-
return n <= 0;
69-
}
70-
}
7130
for (int i = 0; i < flowerbed.length; i++) {
72-
if (i == 0) {
73-
if (flowerbed[i] == 0 && (i + 1) < flowerbed.length && flowerbed[i + 1] == 0) {
74-
n--;
75-
flowerbed[i] = 1;
76-
}
77-
} else if (i < flowerbed.length - 1) {
78-
if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0 && flowerbed[i] == 0) {
79-
n--;
80-
flowerbed[i] = 1;
81-
}
82-
} else if (flowerbed[i] == 0) {
83-
if (flowerbed[i - 1] == 0) {
84-
n--;
85-
flowerbed[i] = 1;
31+
boolean emptyLeftPlot = i == 0 || flowerbed[i - 1] == 0;
32+
boolean emptyRightPlot = i == flowerbed.length - 1 || flowerbed[i + 1] == 0;
33+
if (emptyLeftPlot && emptyRightPlot && flowerbed[i] == 0) {
34+
flowerbed[i] = 1;
35+
n--;
36+
if (n <= 0) {
37+
return true;
8638
}
8739
}
8840
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
public class _605Test {
1010
private _605.Solution1 solution1;
1111
private _605.Solution2 solution2;
12-
private _605.Solution3 solution3;
1312
private static int[] flowerbed;
1413
private static int n;
1514

1615
@BeforeEach
1716
public void setup() {
1817
solution1 = new _605.Solution1();
1918
solution2 = new _605.Solution2();
20-
solution3 = new _605.Solution3();
2119
}
2220

2321
@Test
@@ -159,11 +157,4 @@ public void test20() {
159157
n = 1;
160158
assertEquals(true, solution2.canPlaceFlowers(flowerbed, n));
161159
}
162-
163-
@Test
164-
public void test21() {
165-
flowerbed = new int[] {0};
166-
n = 1;
167-
assertTrue(solution3.canPlaceFlowers(flowerbed, n));
168-
}
169160
}

0 commit comments

Comments
 (0)