Skip to content

Commit ec47cff

Browse files
authored
Merge pull request #854 from 0xff-dev/752
Add solution and test-cases for problem 752
2 parents 66aca24 + 155aa22 commit ec47cff

File tree

3 files changed

+102
-25
lines changed

3 files changed

+102
-25
lines changed

leetcode/701-800/0752.Open-the-Lock/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [752.Open the Lock][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+
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: `'0'`, `'1'`, `'2'`, `'3'`, `'4'`, `'5'`, `'6'`, `'7'`, `'8'`, `'9'`. The wheels can rotate freely and wrap around: for example we can turn `'9'` to be `'0'`, or `'0'` to be `'9'`. Each move consists of turning one wheel one slot.
5+
6+
The lock initially starts at `'0000'`, a string representing the state of the 4 wheels.
7+
8+
You are given a list of `deadends` dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.
9+
10+
Given a `target` representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
16+
Output: 6
17+
Explanation:
18+
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
19+
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
20+
because the wheels of the lock become stuck after the display becomes the dead end "0102".
1321
```
1422

15-
## 题意
16-
> ...
17-
18-
## 题解
23+
**Example 2:**
1924

20-
### 思路1
21-
> ...
22-
Open the Lock
23-
```go
2425
```
26+
Input: deadends = ["8888"], target = "0009"
27+
Output: 1
28+
Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
35+
Output: -1
36+
Explanation: We cannot reach the target without getting stuck.
37+
```
2638

2739
## 结语
2840

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

3-
func Solution(x bool) bool {
4-
return x
3+
type state752 struct {
4+
cur [4]byte
5+
cnt int
6+
}
7+
8+
// "0201","0101","0102","1212","2002"
9+
// 0202
10+
// 0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
11+
func Solution(deadends []string, target string) int {
12+
dm := make(map[[4]byte]struct{})
13+
for _, d := range deadends {
14+
x := [4]byte{}
15+
for i := 0; i < 4; i++ {
16+
x[i] = d[i]
17+
}
18+
dm[x] = struct{}{}
19+
}
20+
var (
21+
add func(byte) byte
22+
del func(byte) byte
23+
)
24+
add = func(a byte) byte {
25+
if a == '9' {
26+
return '0'
27+
}
28+
return a + 1
29+
}
30+
del = func(a byte) byte {
31+
if a == '0' {
32+
return '9'
33+
}
34+
return a - 1
35+
}
36+
37+
state := [4]byte{target[0], target[1], target[2], target[3]}
38+
queue := []state752{{state, 0}}
39+
used := make(map[[4]byte]struct{})
40+
used[state] = struct{}{}
41+
for len(queue) > 0 {
42+
nq := make([]state752, 0)
43+
for _, cur := range queue {
44+
if _, ok := dm[cur.cur]; ok {
45+
continue
46+
}
47+
if cur.cur[0] == '0' && cur.cur[1] == '0' && cur.cur[2] == '0' && cur.cur[3] == '0' {
48+
return cur.cnt
49+
}
50+
51+
for i := 0; i < 4; i++ {
52+
ss := cur.cur[i]
53+
source := cur.cur
54+
source[i] = add(ss)
55+
if _, ok := used[source]; !ok {
56+
used[source] = struct{}{}
57+
nq = append(nq, state752{cur: source, cnt: cur.cnt + 1})
58+
}
59+
source[i] = del(ss)
60+
if _, ok := used[source]; !ok {
61+
used[source] = struct{}{}
62+
nq = append(nq, state752{cur: source, cnt: cur.cnt + 1})
63+
}
64+
}
65+
}
66+
queue = nq
67+
}
68+
return -1
569
}

leetcode/701-800/0752.Open-the-Lock/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
target string
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"0201", "0101", "0102", "1212", "2002"}, "0202", 6},
18+
{"TestCase2", []string{"8888"}, "0009", 1},
19+
{"TestCase3", []string{"8887", "8889", "8878", "8898", "8788", "8988", "7888", "9888"}, "8888", -1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.target)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.target)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)