Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6321215

Browse files
author
Shuo
authored
Merge pull request #241 from openset/develop
Add: Can Place Flowers
2 parents 7e7ca64 + 9e00c7c commit 6321215

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
package can_place_flowers
2+
3+
func canPlaceFlowers(flowerbed []int, n int) bool {
4+
sum, ec := 0, 1
5+
for _, v := range flowerbed {
6+
if v == 0 {
7+
ec++
8+
} else {
9+
sum += (ec - 1) / 2
10+
ec = 0
11+
}
12+
}
13+
sum += ec / 2
14+
return sum >= n
15+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
package can_place_flowers
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
flowerbed []int
7+
n int
8+
expected bool
9+
}
10+
11+
func TestCanPlaceFlowers(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
flowerbed: []int{0, 1, 0, 0, 0, 0, 1, 0},
15+
n: 2,
16+
expected: false,
17+
},
18+
{
19+
flowerbed: []int{1, 0, 0, 0, 1},
20+
n: 1,
21+
expected: true,
22+
},
23+
{
24+
flowerbed: []int{1, 0, 0, 0, 1},
25+
n: 2,
26+
expected: false,
27+
},
28+
{
29+
flowerbed: []int{0, 0, 1, 0},
30+
n: 1,
31+
expected: true,
32+
},
33+
{
34+
flowerbed: []int{0, 1, 0, 0},
35+
n: 1,
36+
expected: true,
37+
},
38+
{
39+
flowerbed: []int{0, 1, 0, 0, 1, 0},
40+
n: 1,
41+
expected: false,
42+
},
43+
{
44+
flowerbed: []int{0, 0, 0, 0, 0},
45+
n: 3,
46+
expected: true,
47+
},
48+
{
49+
flowerbed: []int{0, 0},
50+
n: 2,
51+
expected: false,
52+
},
53+
{
54+
flowerbed: []int{0},
55+
n: 1,
56+
expected: true,
57+
},
58+
{
59+
flowerbed: []int{0, 1},
60+
n: 1,
61+
expected: false,
62+
},
63+
}
64+
for _, tc := range tests {
65+
output := canPlaceFlowers(tc.flowerbed, tc.n)
66+
if output != tc.expected {
67+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.flowerbed, tc.n, output, tc.expected)
68+
}
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /