Skip to content

Commit 7859b0a

Browse files
author
Shuo
authored
Merge pull request #642 from openset/develop
Add: 2 Keys Keyboard
2 parents 470b6c2 + 056d209 commit 7859b0a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
package p_2_keys_keyboard
2+
3+
func minSteps(n int) int {
4+
if n == 1 {
5+
return 0
6+
}
7+
for i := 2; i < n; i++ {
8+
if n%i == 0 {
9+
return i + minSteps(n/i)
10+
}
11+
}
12+
return n
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
package p_2_keys_keyboard
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected int
8+
}
9+
10+
func TestMinSteps(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 1,
14+
expected: 0,
15+
},
16+
{
17+
input: 3,
18+
expected: 3,
19+
},
20+
{
21+
input: 30,
22+
expected: 10,
23+
},
24+
{
25+
input: 97,
26+
expected: 97,
27+
},
28+
}
29+
for _, tc := range tests {
30+
output := minSteps(tc.input)
31+
if output != tc.expected {
32+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)