Minimum Distance to Type a Word Using Two Fingers

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.

思路:記憶化搜索,top down dp,兩個手指move,可以寫一個cost function計算兩個點的cost,這個沒有問題,那麼問題轉換成下一個move由誰來完成,cost分別是多少。top down就是最後一步,如果由左邊完成,cost + dfs(左邊), 由右邊完成,cost + dfs(右邊)建立一個三維的dp,[leftindex][rightindex][pos in word]來標記計算過的結果;Time ( n * 27 ^ 2); Space ( n * 27 ^ 2);

class Solution {
    
    public int minimumDistance(String word) {
        int n = word.length();
        int[][][] dp = new int[27][27][n]; // 27 主要是爲了 null的時候,爲0,從而開一個空的char在0index;
        return dfs(word, dp, 0, null, null);
    }
    
    private int dfs(String word, int[][][] dp, int pos, Character c1, Character c2) {
        if(pos >= word.length()) {
            return 0;
        }
        int index1 = c1 == null ? 0 : c1 - 'A' + 1;
        int index2 = c2 == null ? 0 : c2 - 'A' + 1;
        if(dp[index1][index2][pos] > 0) {
            return dp[index1][index2][pos];
        }
        char next = word.charAt(pos);
        dp[index1][index2][pos] = Math.min(cost(c1, next) + dfs(word, dp, pos + 1, next, c2),
                                          cost(c2, next) + dfs(word, dp, pos + 1, c1, next));
        return dp[index1][index2][pos];
    }
    
    private int cost(Character c1, Character c2) {
        if(c1 == null || c2 == null) {
            return 0;
        }
        int d1 = c1 - 'A', d2 = c2 - 'A';
        int x1 = d1 / 6, y1 = d1 % 6;
        int x2 = d2 / 6, y2 = d2 % 6;
        return Math.abs(x1 - x2) + Math.abs(y1 - y2);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章