1320. Minimum Distance to Type a Word Using Two Fingers Hard

@problem@discussion
#String#Dynamic Programming



1/**
2 * [1320] Minimum Distance to Type a Word Using Two Fingers
3 *
4 * <img alt="" src="https://assets.leetcode.com/uploads/2020/01/02/leetcode_keyboard.png" style="width: 349px; height: 209px;" />
5 * You have a keyboard layout as shown above in the X-Y plane, where each English uppercase letter is located at some coordinate.
6 * 
7 * 	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).
8 * 
9 * Given the string word, return the minimum total distance to type such string using only two fingers.
10 * The distance between coordinates (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|.
11 * 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.
12 *  
13 * Example 1:
14 * 
15 * Input: word = "CAKE"
16 * Output: 3
17 * Explanation: Using two fingers, one optimal way to type "CAKE" is: 
18 * Finger 1 on letter 'C' -> cost = 0 
19 * Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2 
20 * Finger 2 on letter 'K' -> cost = 0 
21 * Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1 
22 * Total distance = 3
23 * 
24 * Example 2:
25 * 
26 * Input: word = "HAPPY"
27 * Output: 6
28 * Explanation: Using two fingers, one optimal way to type "HAPPY" is:
29 * Finger 1 on letter 'H' -> cost = 0
30 * Finger 1 on letter 'A' -> cost = Distance from letter 'H' to letter 'A' = 2
31 * Finger 2 on letter 'P' -> cost = 0
32 * Finger 2 on letter 'P' -> cost = Distance from letter 'P' to letter 'P' = 0
33 * Finger 1 on letter 'Y' -> cost = Distance from letter 'A' to letter 'Y' = 4
34 * Total distance = 6
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	2 <= word.length <= 300
40 * 	word consists of uppercase English letters.
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/
46// discuss: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn minimum_distance(word: String) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_1320() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.