LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers

1320. Minimum Distance to Type a Word Using Two Fingers

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 ( x 1 , y 1 ) (x_1, y_1) (x1,y1) and ( x 2 , y 2 ) (x_2, y_2) (x2,y2) is ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ |x_1 - x_2| + |y_1 - y_2| ∣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.

From: LeetCode

Link: 1320. Minimum Distance to Type a Word Using Two Fingers


Solution:

Ideas:

After typing each character:

  • One finger is always on the most recently typed letter.
  • The DP state stores the position of the other finger.
  • Position 26 means that the second finger has not yet been used.

For every next letter, there are two choices:

  1. Move the finger on the previous letter.
  2. Move the other finger.

Because an unused finger can initially be placed anywhere, moving it has a cost of 0.

Code:
c 复制代码
#include <string.h>
#include <limits.h>

#define UNUSED 26

static int distanceBetween(int a, int b) {
    if (a == UNUSED || b == UNUSED) {
        return 0;
    }

    int rowA = a / 6;
    int colA = a % 6;
    int rowB = b / 6;
    int colB = b % 6;

    int rowDistance = rowA > rowB ? rowA - rowB : rowB - rowA;
    int colDistance = colA > colB ? colA - colB : colB - colA;

    return rowDistance + colDistance;
}

int minimumDistance(char* word) {
    int length = strlen(word);
    int dp[27];
    int nextDp[27];

    /*
     * The previous typed letter is occupied by one finger.
     * dp[i] is the minimum cost when the other finger is at i.
     * Position 26 means that the other finger has not been used.
     */
    for (int i = 0; i < 27; i++) {
        dp[i] = INT_MAX;
    }

    dp[UNUSED] = 0;

    for (int i = 1; i < length; i++) {
        int previous = word[i - 1] - 'A';
        int current = word[i] - 'A';

        for (int j = 0; j < 27; j++) {
            nextDp[j] = INT_MAX;
        }

        for (int other = 0; other < 27; other++) {
            if (dp[other] == INT_MAX) {
                continue;
            }

            /*
             * Use the finger currently on the previous letter.
             * The other finger remains unchanged.
             */
            int cost = dp[other] + distanceBetween(previous, current);

            if (cost < nextDp[other]) {
                nextDp[other] = cost;
            }

            /*
             * Use the other finger to type the current letter.
             * The finger on the previous letter becomes the other finger.
             */
            cost = dp[other] + distanceBetween(other, current);

            if (cost < nextDp[previous]) {
                nextDp[previous] = cost;
            }
        }

        for (int j = 0; j < 27; j++) {
            dp[j] = nextDp[j];
        }
    }

    int answer = INT_MAX;

    for (int i = 0; i < 27; i++) {
        if (dp[i] < answer) {
            answer = dp[i];
        }
    }

    return answer;
}
相关推荐
发疯的技术鼠26 分钟前
第21篇-联合和枚举
c语言
青少儿编程课堂2 小时前
蓝桥杯青少组图形化真题:校园无人机编队灯光秀解析(全国青少年信息素养大赛·电子学会12月图形化考级同源考点)
c++·python·算法·bfs·信息学竞赛
Shadow(⊙o⊙)3 小时前
专题六:模拟算法
算法
23图13 小时前
C 语言开散列表(拉链法)拆解:链挂得没错,key=-1 会去读 T[-1]
c语言·开发语言·散列表
小飞在努力14 小时前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
民乐团扒谱机17 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
我是码代码17 小时前
高分辨率ADC布局详解:走线压降、回流路径与地平面处理
linux·c语言·嵌入式
饮水桶_T17 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法