Minimum Distance to Type a Word Using Two Fingers Problem
Description
LeetCode Problem 1320.

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 (x_1, y_1) and (x_2, y_2) is |x_1 - x_2| + |y_1 - y_2|.
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.
Solution
Approach: Dynamic Programming on the Other Finger
Letter v (with 'A' as 0) sits at row v / 6 and column v % 6, so the distance between two letters is easy to compute. The question is which finger types each letter.
After typing word[i], one finger is certainly resting on word[i]. The only thing we do not know is where the other finger is: on one of the 26 letters, or not placed yet. That gives 27 states. Let dp[j] be the least total distance so far with the other finger on letter j (with j = 26 meaning it has not been used).
At the start, after the first letter, only dp[26] = 0 is possible. To type the next letter cur, coming from prev = word[i - 1], there are two choices:
- The finger on
prevmoves tocur. The other finger stays onj, so the newdp[j]isdp[j] + dist(prev, cur). - The other finger moves from
jtocur. Now the finger left behind is the one onprev, so this updates the newdp[prev]with the bestdp[j] + dist(j, cur)over allj.
The tricky part is the free start: moving a finger that has not been placed yet costs nothing, so dist(26, cur) = 0. This is how the second finger can begin anywhere for free. The answer is the smallest dp value after the last letter.
Complexity: O(26·n) time for a word of length n, and O(1) extra space (two arrays of 27 values).
class Solution:
def minimumDistance(self, word: str) -> int:
FREE = 26 # the second finger has not been placed yet
def dist(a: int, b: int) -> int:
if a == FREE:
return 0 # placing a finger for the first time costs nothing
return abs(a // 6 - b // 6) + abs(a % 6 - b % 6)
INF = 10**9
# dp[j]: the least cost so far, with one finger on the last typed letter
# and the other finger on letter j (or FREE).
dp = [INF] * 27
dp[FREE] = 0
for i in range(1, len(word)):
prev, cur = ord(word[i - 1]) - ord("A"), ord(word[i]) - ord("A")
# The finger on prev types cur; the other one stays on j.
nxt = [dp[j] + dist(prev, cur) for j in range(27)]
# The other finger (on j) types cur; the finger on prev stays there.
nxt[prev] = min(nxt[prev], min(dp[j] + dist(j, cur) for j in range(27)))
dp = nxt
return min(dp)