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

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:

``````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.

``````class Solution {
public:
int minimumDistance(string word) {
vector<vector<vector<int>>> dp(27, vector<vector<int>>(27, vector<int>(301, -1)));
return dfs(word, 26, 26, 0, dp);
}
int dfs(string word, int left, int right, int pos, vector<vector<vector<int>>>& dp) {
if (pos >= word.size()) return 0;
if (dp[left][right][pos] == -1) {
int to = word[pos] - 'A';
dp[left][right][pos] = min(cost(left, to) + dfs(word, to, right, pos + 1, dp), cost(right, to) + dfs(word, left, to, pos + 1, dp));
}
return dp[left][right][pos];
}
int cost(int from, int to) {
return from == 26 ? 0 : abs(from / 6 - to / 6) + abs(from % 6 - to % 6);
}
};
``````

``````class Solution {
public:
int minimumDistance(string word) {
vector<vector<int>> dp(27, vector<int>(301, -1));
return dfs(word, 26, 1, dp);
}
int dfs(string word, int other, int pos, vector<vector<int>>& dp) {
if (pos >= word.size()) return 0;
if (dp[other][pos] == -1) {
int to = word[pos] - 'A', last = word[pos - 1] - 'A';
dp[other][pos] = min(cost(last, to) + dfs(word, other, pos + 1, dp), cost(other, to) + dfs(word, last, pos + 1, dp));
}
return dp[other][pos];
}
int cost(int from, int to) {
return from == 26 ? 0 : abs(from / 6 - to / 6) + abs(from % 6 - to % 6);
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1320

Minimum Time to Type Word Using Special Typewriter

https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/description/

https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/solutions/477659/4-dp-solutions/

https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/solutions/477652/java-c-python-1d-dp-o-1-space/

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation