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