-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1320. Minimum Distance to Type a Word Using Two Fingers (Dynamic Programming) 20.6.23 Hard
More file actions
85 lines (67 loc) · 2.8 KB
/
1320. Minimum Distance to Type a Word Using Two Fingers (Dynamic Programming) 20.6.23 Hard
File metadata and controls
85 lines (67 loc) · 2.8 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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: O(N) T and O(1) S
'''
Base case: When there is only 1 character to type, we only use 1 finger.
For new case n, we expand each case n-1 with 2 possibilities
type the new character using finger 1
type the new character using finger 2
if finger 2 is free, no additional distance is added
'''
class Solution(object):
def minimumDistance(self, word):
"""
:type word: str
:rtype: int
"""
dp = {(self.to_num(word[0]), -1) : 0}
for n in [self.to_num(c) for c in word[1:]]:
new_dp = {}
for (f1, f2), cost in dp.items():
# 对于同时走到这步的,互相比较取最小
new_dp[(f1, n)] = min(new_dp.get((f1, n), float('inf')), cost + self.get_distance(f2, n))
new_dp[(n, f2)] = min(new_dp.get((n, f2), float('inf')), cost + self.get_distance(f1, n))
dp = new_dp
return min(dp.values())
def to_num(self, c):
return ord(c) - ord('A')
def get_distance(self, current_pos, next_pos):
if current_pos == -1: return 0
return abs(current_pos // 6 - next_pos // 6) + abs(current_pos % 6 - next_pos % 6)