You have a keyboard layout as shown above in the X-Y plane, where each English uppercase letter is located at some coordinate.
For example, the letter ‘A’ is located at coordinate (0, 0), the letter ‘B’ is located at coordinate (0, 1), the letter ‘P’ is located at coordinate (2, 3) and the letter ‘Z’ is located at coordinate (4, 1).
Given the string word, return the minimum total distance to type such string using only two fingers.
The distance between coordinates (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|.
Note that the initial positions of your two fingers are considered free so do not count towards your total distance, also your two fingers do not have to start at the first letter or the first two letters.
Example 1:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x17BR1pT-1671420074037)(https://assets.leetcode.com/uploads/2020/01/02/leetcode_keyboard.png)]
Input: word = “CAKE”
Output: 3
Explanation:
Using two fingers, one optimal way to type “CAKE” is:
Finger 1 on letter ‘C’ -> cost = 0
Finger 1 on letter ‘A’ -> cost = Distance from letter ‘C’ to letter ‘A’ = 2
Finger 2 on letter ‘K’ -> cost = 0
Finger 2 on letter ‘E’ -> cost = Distance from letter ‘K’ to letter ‘E’ = 1
Total distance = 3
Example 2:
Input: word = “HAPPY”
Output: 6
Explanation:
Using two fingers, one optimal way to type “HAPPY” is:
Finger 1 on letter ‘H’ -> cost = 0
Finger 1 on letter ‘A’ -> cost = Distance from letter ‘H’ to letter ‘A’ = 2
Finger 2 on letter ‘P’ -> cost = 0
Finger 2 on letter ‘P’ -> cost = Distance from letter ‘P’ to letter ‘P’ = 0
Finger 1 on letter ‘Y’ -> cost = Distance from letter ‘A’ to letter ‘Y’ = 4
Total distance = 6
Constraints:
- 2 <= word.length <= 300
- word consists of uppercase English letters.
跟昨天的题差不多, 但是要简单很多, 开始两个手指可以处在任意位置上, 每个字母要么是第一个手指按要么是第二个手指按, 我们将 dp(i, r1, c1, r2, c2)表示为按第 i 个字母前手指 1 处于(r1, c1), 手指 2 处于(r2, c2)时最小的距离, 那 dp(i, r1, c2, r2, c2) = min(dp(i, row, col, r2, c2) + |r1 - row| + |c1 - col|, dp(i, r1, c1, row, col) + |r2 - row| + |c2 - col|), 其中 row 和 col 分别是 word[i]所处的行跟列的 index, 注意我们开始的时候可以从任意键开始按, 所以两根手指的第一次移动距离一定是 0
use std::collections::HashMap;
impl Solution {
fn dp(word: &Vec<char>, idx: usize, r1: i32, c1: i32, r2: i32, c2: i32, cache: &mut HashMap<(usize, i32, i32, i32, i32), i32>) -> i32 {
if idx == word.len() {
return 0;
}
let ord = word[idx] as i32 - 65;
let row = ord / 6;
let col = ord % 6;
let mut dist1 = (r1 - row).abs() + (c1 - col).abs();
if dist1 > 10 {
dist1 = 0;
}
let next1 = if let Some(c) = cache.get(&(idx + 1, row, col, r2, c2)) {
*c
} else {
Solution::dp(word, idx + 1, row, col, r2, c2, cache)
};
let mut dist2 = (r2 - row).abs() + (c2 - col).abs();
if dist2 > 10 {
dist2 = 0;
}
let next2 = if let Some(c) = cache.get(&(idx + 1, r1, c1, row, col)) {
*c
} else {
Solution::dp(word, idx + 1, r1, c1, row, col, cache)
};
let ans = (dist1 + next1).min(dist2 + next2);
cache.insert((idx, r1, c1, r2, c2), ans);
ans
}
pub fn minimum_distance(word: String) -> i32 {
Solution::dp(&word.chars().collect(), 0, 100, 100, 100, 100, &mut HashMap::new())
}
}