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 aef1ebe

Browse files
author
Shuo
authored
Merge pull request #677 from openset/develop
Add: Arranging Coins
2 parents 289e706 + 4f9394f commit aef1ebe

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
package arranging_coins
1+
package problem_441
2+
3+
import "math"
4+
5+
func arrangeCoins(n int) int {
6+
x := math.Sqrt(1+8*float64(n)) - 1
7+
return int(x) / 2
8+
}
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
package arranging_coins
1+
package problem_441
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected int
8+
}
9+
10+
func TestArrangeCoins(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 5,
14+
expected: 2,
15+
},
16+
{
17+
input: 8,
18+
expected: 3,
19+
},
20+
{
21+
input: 0,
22+
expected: 0,
23+
},
24+
{
25+
input: 1,
26+
expected: 1,
27+
},
28+
{
29+
input: 2,
30+
expected: 1,
31+
},
32+
{
33+
input: 3,
34+
expected: 2,
35+
},
36+
{
37+
input: 13,
38+
expected: 4,
39+
},
40+
{
41+
input: 130,
42+
expected: 15,
43+
},
44+
}
45+
for _, tc := range tests {
46+
output := arrangeCoins(tc.input)
47+
if output != tc.expected {
48+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
49+
}
50+
}
51+
}

0 commit comments

Comments
(0)

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