Skip to main content
deleted 11 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I'm posting my Python code for LeetCode's 1320. If you have time and would like to review, please do so. Thank you!

I'm posting my Python code for LeetCode's 1320. If you have time and would like to review, please do so. Thank you!

I'm posting my Python code for LeetCode's 1320. If you have time and would like to review, please do so.

Source Link
Emma Marcier
  • 3.7k
  • 3
  • 14
  • 43

LeetCode 1320: Minimum Distance to Type a Word Using Two Fingers II

I'm posting my Python code for LeetCode's 1320. If you have time and would like to review, please do so. Thank you!

Problem

enter image description here

  • 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 \$(x_1,y_1)\$ and \$(x_2,y_2)\$ is \$|x_1 - x_2| + |y_1 - y_2|\$.

  • 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

Constraints:

  • \$2 \le \text{word.length} \le 300\$
  • Each word[i] is an English uppercase letter.

Accepted Python

import string
import functools

class Solution:
    def minimumDistance(self, word: str) -> int:
        WIDTH = 6
        A_DECIMAL = 65
        def get_coordinates(left: int, right: int) -> int:
            if not left or not right:
                return 0

            x_left, y_left = indices[left]
            x_right, y_right = indices[right]

            return abs(x_left - x_right) + abs(y_left - y_right)

        @functools.lru_cache(None)
        def get_dp(index, word_a: str = None, word_b: str = None) -> None:
            if index == len(chars):
                return 0

            dist_a = get_coordinates(word_a, chars[index]) + get_dp(index + 1, chars[index], word_b)
            dist_b = get_coordinates(word_b, chars[index]) + get_dp(index + 1, word_a, chars[index])

            return min(dist_a, dist_b)

        chars = ''.join(a for a, b in zip(word, word[1:] + ' ') if a != b)
        indices = {char: divmod((ord(char) - A_DECIMAL), WIDTH) for char in string.ascii_uppercase}
        return get_dp(0)

Reference

On LeetCode, there is a class usually named Solution with one or more public functions which we are not allowed to rename.