Description
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 <= 300wordconsists of uppercase English letters.
Solutions
This solution uses dynamic programming to efficiently find the minimum distance needed to type a word with two fingers. The dp[i][j] table stores the minimum cost to type the first i characters with one finger at letter j, while the other finger can be at any position. For each character, the algorithm considers two cases: keeping the previous finger at position j and moving the other finger to the current character, or moving the finger that was at j to the current character. The getDistance function computes Manhattan distance on the keyboard grid, treating each letter as a coordinate position (row and column).
/**
* @param {string} word
* @return {number}
*/
var minimumDistance = function (word) {
const n = word.length;
const getDistance = (p, q) => {
const x1 = Math.floor(p / 6),
y1 = p % 6;
const x2 = Math.floor(q / 6),
y2 = q % 6;
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
};
const dp = Array.from({ length: n }, () => Array.from({ length: 26 }, () => Math.floor(Number.MAX_SAFE_INTEGER / 2)));
for (let j = 0; j < 26; j++) {
dp[0][j] = 0;
}
for (let i = 1; i < n; i++) {
const cur = word.charCodeAt(i) - 65;
const prev = word.charCodeAt(i - 1) - 65;
const d = getDistance(prev, cur);
for (let j = 0; j < 26; j++) {
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + d);
if (prev === j) {
for (let k = 0; k < 26; k++) {
const d0 = getDistance(k, cur);
dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + d0);
}
}
}
}
let ans = Math.floor(Number.MAX_SAFE_INTEGER / 2);
for (let j = 0; j < 26; j++) {
ans = Math.min(ans, dp[n - 1][j]);
}
return ans;
};