Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 33f8ffd

Browse files
aQuaaQua
aQua
authored and
aQua
committed
650 finish
1 parent 2bb965f commit 33f8ffd

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Problem0650
2+
3+
func minSteps(n int) int {
4+
if n == 1 {
5+
return 0
6+
}
7+
8+
for i := 2; i < n; i++ {
9+
if n%i == 0 {
10+
return i + minSteps(n/i)
11+
}
12+
}
13+
14+
return n
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Problem0650
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
n int
13+
ans int
14+
}{
15+
16+
{
17+
100,
18+
14,
19+
},
20+
21+
{
22+
1,
23+
0,
24+
},
25+
26+
{
27+
3,
28+
3,
29+
},
30+
31+
// 可以有多个 testcase
32+
}
33+
34+
func Test_fn(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, tc := range tcs {
38+
fmt.Printf("~~%v~~\n", tc)
39+
ast.Equal(tc.ans, minSteps(tc.n), "输入:%v", tc)
40+
}
41+
}
42+
43+
func Benchmark_fn(b *testing.B) {
44+
for i := 0; i < b.N; i++ {
45+
for _, tc := range tcs {
46+
minSteps(tc.n)
47+
}
48+
}
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [650. 2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/)
2+
3+
## 题目
4+
5+
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
6+
7+
Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
8+
9+
Paste: You can paste the characters which are copied last time.
10+
11+
Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.
12+
13+
Example 1:
14+
15+
```text
16+
Input: 3
17+
Output: 3
18+
Explanation:
19+
Intitally, we have one character 'A'.
20+
In step 1, we use Copy All operation.
21+
In step 2, we use Paste operation to get 'AA'.
22+
In step 3, we use Paste operation to get 'AAA'.
23+
```
24+
25+
Note:
26+
The n will be in the range [1, 1000].
27+
28+
29+
## 解题思路
30+
31+
见程序注释

0 commit comments

Comments
 (0)