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.
Solution: DP
Top down: O(n*27^2)
C++
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 |
// Author: Huahua, 64 ms, 46.6 MB class Solution { public: int minimumDistance(string word) { constexpr int kRest = 26; const int n = word.length(); vector<vector<vector<int>>> mem(n, vector<vector<int>>(27, vector<int>(27, -1))); auto cost = [](int c1, int c2) { if (c1 == kRest) return 0; return abs(c1 / 6 - c2 / 6) + abs(c1 % 6 - c2 % 6); }; // min cost to type word[i:n]. l, r are the last finger positions. function<int(int, int, int)> dp = [&](int i, int l, int r) { if (i == n) return 0; if (mem[i][l][r] >= 0) return mem[i][l][r]; int c = word[i] - 'A'; return mem[i][l][r] = min(dp(i + 1, c, r) + cost(l, c), dp(i + 1, l, c) + cost(r, c)); }; return dp(0, kRest, kRest); } }; |
C++
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 |
// Author: Huahua, 12 ms, 10.1 MB class Solution { public: int minimumDistance(string word) { constexpr int kRest = 26; const int n = word.length(); vector<vector<int>> mem(n, vector<int>(27)); auto cost = [](int c1, int c2) { if (c1 == kRest) return 0; return abs(c1 / 6 - c2 / 6) + abs(c1 % 6 - c2 % 6); }; // min cost to type word[i:n]. o is the last position of the other finger. // the current finger is at word[i - 1]. function<int(int, int)> dp = [&](int i, int o) { if (i == n) return 0; if (mem[i][o]) return mem[i][o]; int p = i == 0 ? kRest : word[i - 1] - 'A'; int c = word[i] - 'A'; return mem[i][o] = min(dp(i + 1, o) + cost(p, c), // same finger dp(i + 1, p) + cost(o, c)); // other finger }; return dp(0, kRest); } }; |
C++
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 |
// Author: Huahua, 8 ms, 10.1 MB class Solution { public: int minimumDistance(string word) { constexpr int kRest = 26; const int n = word.length(); // dp[i][j] : min cost to type word[0:i] // one finger is at word[i - 1], another finger is at j. vector<vector<int>> dp(n + 1, vector<int>(27, INT_MAX / 2)); dp[0][kRest] = 0; auto cost = [](int c1, int c2) { if (c1 == kRest) return 0; return abs(c1 / 6 - c2 / 6) + abs(c1 % 6 - c2 % 6); }; for (int i = 0; i < n; ++i) { int p = i == 0 ? kRest : word[i - 1] - 'A'; int c = word[i] - 'A'; for (int j = 0; j <= 26; ++j) { dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cost(p, c)); // same finger dp[i + 1][p] = min(dp[i + 1][p], dp[i][j] + cost(j, c)); // other finger } } return *min_element(begin(dp[n]), end(dp[n])); } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment