Skip to content

Commit de3e08f

Browse files
committed
Added S605 solution for leetcode
1 parent d821fc0 commit de3e08f

3 files changed

Lines changed: 83 additions & 11 deletions

File tree

LeetCode/LeetCode.iml

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package problem.algorithm;
2+
3+
public class S605 {
4+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
5+
int max = 0;
6+
7+
int i = 0;
8+
while (i < flowerbed.length && flowerbed[i] == 0) i ++;
9+
// make i = 1
10+
max += i/ 2;
11+
if (i == flowerbed.length) max = (flowerbed.length + 1) / 2;
12+
13+
while (i < flowerbed.length) {
14+
// from having flower
15+
while (i < flowerbed.length && flowerbed[i] == 1) i++;
16+
// make i = 0
17+
int beg1 = i;
18+
19+
while (i < flowerbed.length && flowerbed[i] == 0) i++;
20+
// make i = 1
21+
if (i == flowerbed.length) i ++;
22+
max += (i - beg1 - 1) / 2;
23+
}
24+
25+
return max >= n;
26+
}
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package problem.algorithm;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
public class S605Test {
7+
8+
@Test
9+
public void testExample1() {
10+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{1, 0, 0, 0, 1}, 1), true);
11+
}
12+
13+
@Test
14+
public void testExample2() {
15+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{1, 0, 0, 0, 1}, 2), false);
16+
}
17+
18+
@Test
19+
public void testCustom1() {
20+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{1, 0, 0, 1, 0, 0}, 1), true);
21+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{1, 0, 0, 1, 0, 0}, 2), false);
22+
}
23+
24+
@Test
25+
public void testCustom2() {
26+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 0, 1, 0, 0, 1, 1}, 1), true);
27+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 0, 1, 0, 0, 1, 1}, 2), false);
28+
}
29+
30+
@Test
31+
public void testCustom3() {
32+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 1, 0, 0, 1, 1}, 0), true);
33+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 1, 0, 0, 1, 1}, 1), false);
34+
}
35+
36+
@Test
37+
public void testRegression1() {
38+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0}, 1), true);
39+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{1}, 1), false);
40+
}
41+
42+
@Test
43+
public void testRegression2() {
44+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 0}, 1), true);
45+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 0}, 2), false);
46+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{1, 0}, 1), false);
47+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 1}, 1), false);
48+
}
49+
50+
51+
@Test
52+
public void testRegression3() {
53+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 0, 0}, 2), true);
54+
Assert.assertEquals(new S605().canPlaceFlowers(new int[]{0, 0, 0}, 3), false);
55+
}
56+
}

0 commit comments

Comments
 (0)