Solving Leetcode Interviews in Seconds with AI: Minimum Distance to Type a Word Using Two Fingers
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1320" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You have a keyboard layout as shown above in the X-Y 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 do not 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 Constraints: 2 <= word.length <= 300 word consists of uppercase English letters.
Explanation
Here's a solution to the problem:
- Dynamic Programming: Use dynamic programming to store the minimum distance to type the first
icharacters of the word, with one finger on letterjand the other on letterk. - State Definition:
dp[i][j][k]represents the minimum cost to type the firstiletters of the word such that one finger is on letterjand another finger is on letterk. Transitions: When typing the
i+1-th letter, we can either move the finger on letterjto thei+1-th letter, or move the finger on letterkto thei+1-th letter, and then update dp array accordingly.Runtime Complexity: O(N * 26 * 26), where N is the length of the word. Storage Complexity: O(N * 26 * 26).
Code
def minimumTotalDistance(word: str) -> int:
"""
Calculates the minimum total distance to type a word using two fingers on a keyboard.
Args:
word: The word to type.
Returns:
The minimum total distance.
"""
positions = {
chr(ord('A') + i): (i // 6, i % 6) for i in range(26)
}
def distance(char1, char2):
if char1 is None or char2 is None:
return 0
x1, y1 = positions[char1]
x2, y2 = positions[char2]
return abs(x1 - x2) + abs(y1 - y2)
n = len(word)
dp = {}
def solve(i, finger1, finger2):
if i == n:
return 0
if (i, finger1, finger2) in dp:
return dp[(i, finger1, finger2)]
char = word[i]
# Move finger1
cost1 = distance(finger1, char) + solve(i + 1, char, finger2)
# Move finger2
cost2 = distance(finger2, char) + solve(i + 1, finger1, char)
dp[(i, finger1, finger2)] = min(cost1, cost2)
return dp[(i, finger1, finger2)]
return solve(0, None, None)