Skip to content

Commit f8c96ee

Browse files
authored
Merge pull request #849 from 0xff-dev/650
Add solution and test-cases for problem 650
2 parents 2b62da3 + 7ea0ad9 commit f8c96ee

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

leetcode/601-700/0650.2-Keys-Keyboard/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [650.2 Keys Keyboard][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is only one character `'A'` on the screen of a notepad. You can perform one of two operations on this notepad for each step:
5+
6+
- Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
7+
- Paste: You can paste the characters which are copied last time.
8+
9+
Given an integer `n`, return the minimum number of operations to get the character `'A'` exactly `n` times on the screen.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: n = 3
15+
Output: 3
16+
Explanation: Initially, we have one character 'A'.
17+
In step 1, we use Copy All operation.
18+
In step 2, we use Paste operation to get 'AA'.
19+
In step 3, we use Paste operation to get 'AAA'.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
2 Keys Keyboard
23-
```go
2424
```
25-
25+
Input: n = 1
26+
Output: 0
27+
```
2628

2729
## 结语
2830

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int {
4+
if n == 1 {
5+
return 0
6+
}
7+
// 存在一个当前复制几个问题
8+
// 1 复制了1次
9+
// dp[i][j] 表示通过复制j个得到i个元素需要的最少次数,0就是无法到达
10+
dp := make([][]int, n+1)
11+
for i := 0; i <= n; i++ {
12+
dp[i] = make([]int, n+1)
13+
if i > 1 {
14+
for j := 0; j <= n; j++ {
15+
dp[i][j] = -1
16+
}
17+
}
18+
}
19+
for i := 2; i <= n; i++ {
20+
// 通过粘贴nci 到达i
21+
for ct := i - 1; ct > 0; ct-- {
22+
start := i - ct // 从那个位置开始
23+
if start > ct && dp[start][ct] != -1 {
24+
if r := dp[start][ct] + 1; dp[i][ct] == -1 || dp[i][ct] > r {
25+
dp[i][ct] = r
26+
}
27+
}
28+
if start == ct {
29+
op := -1
30+
if start == 1 {
31+
op = 0
32+
}
33+
for pn := 1; pn < start; pn++ {
34+
if dp[start][pn] != -1 && (op == -1 || dp[start][pn] < op) {
35+
op = dp[start][pn]
36+
}
37+
}
38+
op += 2 //copy and past
39+
if dp[i][ct] == -1 || dp[i][ct] > op {
40+
dp[i][ct] = op
41+
}
42+
}
43+
}
44+
}
45+
op := -1
46+
for i := 1; i < n; i++ {
47+
if dp[n][i] == -1 {
48+
continue
49+
}
50+
if op == -1 || dp[n][i] < op {
51+
op = dp[n][i]
52+
}
53+
}
54+
return op
555
}

leetcode/601-700/0650.2-Keys-Keyboard/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 3, 3},
17+
{"TestCase2", 1, 0},
18+
{"TestCase3", 99, 17},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)