I'm trying to educate myself better about dynamic programming, and hoping to do so by attempting to solve the following problem (for reference here's a solution to it).
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.
For example for the input word "HAPPY"
we would have:
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
From what I read online there are 1D, 2D and 3D (in terms of space) dynamic programming solutions to the above problem. I have found 1D and 2D solutions online to this problem, but I find them too difficult to follow, so I'm hoping to start with the 3D one and gradually understand more efficient ones.
What's a 3D DP formulation to this problem? Does this problem have a name in particular?
I understand the recursive nature of the problem, but I'm struggling to formulate a simple bottom-up solution (e.g. in 3D).