Skip to content

Commit d2166fb

Browse files
committed
Add solution and test-cases for problem 514
1 parent 09a1dcb commit d2166fb

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

leetcode/501-600/0514.Freedom-Trail/README.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [514.Freedom Trail][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+
In the video game Fallout 4, the quest **"Road to Freedom"** requires players to reach a metal dial called the **"Freedom Trail Ring"** and use the dial to spell a specific keyword to open the door.
75

8-
**Example 1:**
6+
Given a string `ring` that represents the code engraved on the outer ring and another string `key` that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Initially, the first character of the ring is aligned at the `"12:00"` direction. You should spell all the characters in `key` one by one by rotating `ring` clockwise or anticlockwise to make each character of the string key aligned at the `"12:00"` direction and then by pressing the center button.
9+
10+
At the stage of rotating the ring to spell the key character `key[i]`:
11+
12+
1. You can rotate the ring clockwise or anticlockwise by **one place**, which counts as one step. The final purpose of the rotation is to align one of `ring`'s characters at the `"12:00"` direction, where this character must equal `key[i]`.
13+
2. If the character `key[i]` has been aligned at the `"12:00"` direction, press the center button to spell, which also counts as **one step**. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.
1414

15-
## 题意
16-
> ...
15+
**Example 1:**
1716

18-
## 题解
17+
![1](./ring.jpeg)
1918

20-
### 思路1
21-
> ...
22-
Freedom Trail
23-
```go
2419
```
20+
Input: ring = "godding", key = "gd"
21+
Output: 4
22+
Explanation:
23+
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
24+
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
25+
Also, we need 1 more step for spelling.
26+
So the final output is 4.
27+
```
28+
29+
**Example 2:**
2530

31+
```
32+
Input: ring = "godding", key = "godding"
33+
Output: 13
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(ring string, key string) int {
4+
// 感觉是dfs+cache实现
5+
pos := [26][]int{}
6+
for i := 0; i < 26; i++ {
7+
pos[i] = make([]int, 0)
8+
}
9+
for i, r := range ring {
10+
pos[r-'a'] = append(pos[r-'a'], i)
11+
}
12+
lr := len(ring)
13+
lk := len(key)
14+
cache := make([][]int, lr)
15+
for i := 0; i < lr; i++ {
16+
cache[i] = make([]int, lk)
17+
}
18+
19+
var dfs func(int, int) int
20+
dfs = func(keyIndex, ringIndex int) int {
21+
if keyIndex == len(key) {
22+
return 0
23+
}
24+
if cache[ringIndex][keyIndex] != 0 {
25+
return cache[ringIndex][keyIndex]
26+
}
27+
28+
curByte := key[keyIndex]
29+
ans := -1
30+
for _, idx := range pos[curByte-'a'] {
31+
diff := ringIndex - idx
32+
if diff < 0 {
33+
diff = -diff
34+
}
35+
diff = min(lr-diff, diff)
36+
diff++
37+
steps := dfs(keyIndex+1, idx)
38+
if ans == -1 || diff+steps < ans {
39+
ans = diff + steps
40+
}
41+
}
42+
cache[ringIndex][keyIndex] = ans
43+
return ans
44+
}
45+
return dfs(0, 0)
546
}

leetcode/501-600/0514.Freedom-Trail/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
ring, key string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "godding", "gd", 4},
17+
{"TestCase2", "godding", "godding", 13},
18+
{"TestCase3", "pqwcx", "cpqwx", 13},
19+
{"TestCase4", "caotmcaataijjxi", "oatjiioicitatajtijciocjcaaxaaatmctxamacaamjjx", 137},
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.ring, c.key)
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.ring, c.key)
2829
}
2930
})
3031
}
3132
}
3233

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

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

0 commit comments

Comments
 (0)