Skip to content

Commit 50c102c

Browse files
committed
add: 自由之路
1 parent 9f16f29 commit 50c102c

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8
661661

662662
### 其它
663663

664+
- [自由之路](src/search/freedom_trail.rs) [深度优先搜索, 广度优先搜索, 字符串, 动态规划]
665+
666+
- LeetCode 514. 自由之路 <https://leetcode.cn/problems/freedom-trail>
667+
664668
- [赎金信](src/map/ransom_note.rs) [哈希表, 字符串, 计数]
665669

666670
- LeetCode 383. 赎金信 <https://leetcode.cn/problems/ransom-note>

images/search/freedom_trail.jpeg

200 KB
Loading

src/search/freedom_trail.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 自由之路
2+
// https://leetcode.cn/problems/freedom-trail
3+
// INLINE ../../images/search/freedom_trail.jpeg
4+
5+
pub struct Solution;
6+
7+
impl Solution {
8+
pub fn find_rotate_steps(ring: String, key: String) -> i32 {
9+
let mut idx_list: Vec<Vec<usize>> = vec![vec![]; 26];
10+
let n = ring.len();
11+
let m = key.len();
12+
let mut pc = (ring.chars().next().unwrap() as u8 - b'a') as usize;
13+
14+
for (i, c) in ring.chars().enumerate() {
15+
idx_list[(c as u8 - b'a') as usize].push(i);
16+
}
17+
18+
let mut dp: Vec<i32> = vec![0x3f3f3f3f; n];
19+
let mut pre: Vec<i32> = vec![0x3f3f3f3f; n];
20+
pre[0] = 0;
21+
22+
for i in 0..m {
23+
for &idx in &idx_list[key.chars().nth(i).unwrap() as usize - 'a' as usize] {
24+
for &pi in &idx_list[pc] {
25+
dp[idx] = dp[idx].min(
26+
pre[pi]
27+
+ (pi as i32 - idx as i32)
28+
.abs()
29+
.min(n as i32 - (pi as i32 - idx as i32).abs()),
30+
);
31+
}
32+
}
33+
pre = dp.clone();
34+
dp = vec![0x3f3f3f3f; n];
35+
pc = key.chars().nth(i).unwrap() as usize - 'a' as usize;
36+
}
37+
38+
*pre.iter().min().unwrap() + key.len() as i32
39+
}
40+
}

src/search/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
pub mod climbing_stairs;
12
pub mod flip_chess;
3+
pub mod freedom_trail;
24
pub mod minimum_moves_to_move_a_box_to_their_target_location;
35
pub mod number_of_closed_islands;
6+
pub mod path_with_minimum_effort;
47
pub mod pond_sizes_lcci;
58
pub mod shortest_path_in_binary_matrix;
6-
pub mod climbing_stairs;
7-
pub mod path_with_minimum_effort;

tests/search/freedom_trail_test.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use rust_practice::search::freedom_trail::Solution;
2+
3+
#[test]
4+
fn find_rotate_steps_test() {
5+
// 示例 1:
6+
// 输入: ring = "godding", key = "gd"
7+
// 输出: 4
8+
// 解释:
9+
// 对于 key 的第一个字符 'g',已经在正确的位置, 我们只需要1步来拼写这个字符。
10+
// 对于 key 的第二个字符 'd',我们需要逆时针旋转 ring "godding" 2步使它变成 "ddinggo"。
11+
// 当然, 我们还需要1步进行拼写。
12+
// 因此最终的输出是 4。
13+
let ring = "godding".to_string();
14+
let key = "gd".to_string();
15+
assert_eq!(Solution::find_rotate_steps(ring, key), 4);
16+
17+
// 示例 2:
18+
// 输入: ring = "godding", key = "godding"
19+
// 输出: 13
20+
let ring = "godding".to_string();
21+
let key = "godding".to_string();
22+
assert_eq!(Solution::find_rotate_steps(ring, key), 13);
23+
}

tests/search/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod pond_sizes_lcci_test;
55
pub mod shortest_path_in_binary_matrix_test;
66
pub mod climbing_stairs_test;
77
pub mod path_with_minimum_effort_test;
8+
pub mod freedom_trail_test;

0 commit comments

Comments
 (0)