leetcode 1320 - Minimum Distance to Type a Word Using Two Fingers

https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/
https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477652/JavaC%2B%2BPython-DP-Solution-O(1)-Space

ABCDEF
GHIJKL
MNOPQR
STUVWX
YZ

You have a keyboard layout as shown above in the XY 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 don’t 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
Example 3:

Input: word = “NEW”
Output: 3
Example 4:

Input: word = “YEAR”
Output: 7

Constraints:

2 <= word.length <= 300
Each word[i] is an English uppercase letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
int memo[300][27][27];

int minimumDistance(string word) {
for (int i = 0; i < 300; i++)
for (int j = 0; j < 27; j++)
for (int k = 0; k < 27; k++)
memo[i][j][k] = -1;

return dfs(word, 0, {4,2}, {4,2});
}

int dfs(string &word, int i, pair<int,int> finger1, pair<int,int> finger2) {
if (i == (int)word.size()) return 0;

int idx1 = finger1.first * 6 + finger1.second;
int idx2 = finger2.first * 6 + finger2.second;
if (memo[i][idx1][idx2] != -1)
return memo[i][idx1][idx2];

pair<int,int> p = getPoint(word[i]);
int d1 = getDist(p, finger1);
int d2 = getDist(p, finger2);

//finger 1
int dist1 = dfs(word, i+1, p, finger2) + d1;
//finger 2
int dist2 = dfs(word, i+1, finger1, p) + d2;

memo[i][idx1][idx2] = min(dist1, dist2);
return memo[i][idx1][idx2];
}

pair<int,int> getPoint(char c) {
int idx = c - 'A';

return {idx/6, idx%6};
}

int getDist(pair<int,int> &p1, pair<int,int> &p2) {
if (p2.first * 6 + p2.second == 26) return 0;

return abs(p1.first - p2.first) + abs(p1.second - p2.second);
}
};