LeetCode每日一题(1320. Minimum Distance to Type a Word Using Two Fingers)

该博客介绍了如何运用动态规划算法解决一个计算机科学问题:在特定键盘布局下,用两个手指输入字符串时的最短总距离。例子展示了如何计算输入单词如'CAKE'和'HAPPY'的最小距离,并给出了解决此类问题的代码实现,其中涉及到二维平面上的坐标距离计算和利用缓存减少重复计算。
摘要由CSDN通过智能技术生成

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())
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值