2
votes

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

enter image description here

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

1
Interesting problem. I find artificial intelligence algorithms very likely to solve it, but maybe you are looking for a brute force algorithm, are you?alan.elkin

1 Answers

2
votes

A fairly simple 3-D dynamic programming approach is the following:

def dist(a,b): # gets distance between chars a and b
    y1,x1 = a/6,a%6
    y2,x2 = b/6,b%6
    return abs(y1-y2)+abs(x1-x2)

def solve(s):
    N = len(s)
    dp = [[[float('inf') for x in range(26)] for x in range(26)] for x in range(N+1)]
    dp[0] = [[0 for x in range(26)] for x in range(26)]
    for i in range(N):
         cur = ord(s[i])-ord('A')
         for j in range(26):
              for k in range(26):
                   dp[i+1][j][cur] = min(dp[i+1][j][cur], dp[i][j][k] + dist(k,cur)) # move right finger
                   dp[i+1][cur][k] = min(dp[i+1][cur][k], dp[i][j][k] + dist(j,cur)) # move left finger
    res = float('inf')
    for i in dp[N]:
         res = min(res,min(i))
    return res

In the solve function, we declare a dynamic programming table dp[N+1][26][26], and let's store at cell dp[i][j][k] the minimum distance required to type all characters in the string up to but not including the i'th character, with the left finger finishing on the j'th key and the right finger on the k'th key.

Our base case is at i = 0, we know it takes 0 total distance to have our fingers start anywhere, so the first row is initialized entirely with 0s. Then, the transition is from all possible configurations of the two fingers to either moving the left finger or the right finger to the new key. If you're currently at state dp[i][j][k], after pressing the i'th key (let's call it cur), if we press this new key with our left finger, the updated state is dp[i+1][cur][k], since the left finger moved from j to cur. Similarly, if we press it with our right finger, the updated state is dp[i+1][j][cur].

Our final answer is found somewhere in the N+1'th row, where N is the length of the string. We simply take the minimum of all combinations of left and right fingers in this row.

EDIT: Here's the 2-D solution I described in the comments:

def solve(s):
    N = len(s)
    dp = [[float('inf') for x in range(26)]for x in range(N)]
    dp[0] = [0 for x in range(26)]
    for i in range(1,N):
        cur = ord(s[i])-ord('A')
        lst = ord(s[i-1])-ord('A')
        for j in range(26): # one finger currently on s[i-1], other finger on j
             dp[i][j] = min(dp[i][j], dp[i-1][j] + dist(lst,cur)) # move first finger, so second finger remains on j
             dp[i][lst] = min(dp[i][lst], dp[i-1][j] + dist(j,cur))   # move second finger, so second finger becomes the new "first finger"
                                                                                # and now the old "first finger" becomes the new "second finger"
    res = min(dp[N-1])
    return res