LeetCode 72 questions: edit distance (dynamic programming)

Address: https: //leetcode-cn.com/problems/edit-distance

Status: dp[i][j]said it would word1[0, i]be converted into word2[0, j]the number of programs.

State transition equation: Category talk (from the middle to start thinking about the situation).

(1) If word1.charAt(i) == word2.charAt(j)true, then do dp[i][j] = dp[i - 1][j - 1]nothing, ;
(2) otherwise:

① modifications word1[i]become word2[j]; after
② the word1[0, i]future of the last character to delete;
③ will word1[0, i]be added after the end of a character;

Converted from the three cases.

dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
  • Initialization: Here the array dpto open a line, to open one, in order to satisfy the boundary conditions.
// 初始化
for (int i = 0; i <= len1; i++) {
    dp[i][0] = i;
}

for (int j = 0; j <= len2; j++) {
    dp[0][j] = j;
}
  • Output:dp[len1][len2]

  • State Reduction: just look at the current line on the line, so you can use the "scroll array" or compressed directly into a row (difficult, therefore, not compressed).

Java code:

public class Solution4 {

    public int minDistance(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();

        int[][] dp = new int[len1 + 1][len2 + 1];
        
        // 初始化
        for (int i = 0; i <= len1; i++) {
            dp[i][0] = i;
        }

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

        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (word1.charAt(i) == word2.charAt(j)) {
                    dp[i + 1][j + 1] = dp[i][j];
                    continue;
                }
                dp[i + 1][j + 1] = Math.min(dp[i][j + 1], Math.min(dp[i + 1][j], dp[i][j])) + 1;
            }
        }
        return dp[len1][len2];
    }
}

Really do it "state Compression", I found not so easy, because of initialization.

Java code:

import java.util.Arrays;

public class Solution5 {

    public int minDistance(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();

        int[][] dp = new int[len1 + 1][len2 + 1];

        // 状态转移方程:dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
        // 初始化

        for (int i = 0; i <= len1; i++) {
            dp[i][0] = i;
        }

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


        // 打印输出
        for (int i = 0; i < dp.length; i++) {
            System.out.println(Arrays.toString(dp[i]));
        }
        System.out.println();

        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (word1.charAt(i) == word2.charAt(j)) {
                    dp[i + 1][j + 1] = dp[i][j];
                    continue;
                }
                dp[i + 1][j + 1] = Math.min(dp[i][j + 1], Math.min(dp[i + 1][j], dp[i][j])) + 1;
            }
        }

        // 打印输出
        for (int i = 0; i < dp.length; i++) {
            System.out.println(Arrays.toString(dp[i]));
        }

        return dp[len1][len2];
    }


    public static void main(String[] args) {
        Solution5 solution5 = new Solution5();
        String word1 = "horse";
        String word2 = "ros";
        int res = solution5.minDistance(word1, word2);
        System.out.println(res);
    }
}

Output:

[0, 1, 2, 3]
[1, 0, 0, 0]
[2, 0, 0, 0]
[3, 0, 0, 0]
[4, 0, 0, 0]
[5, 0, 0, 0]

[0, 1, 2, 3]
[1, 1, 2, 3]
[2, 2, 1, 2]
[3, 2, 2, 2]
[4, 3, 3, 2]
[5, 4, 4, 3]
3

Description: Dynamic programming problem sometimes, have to start thinking about thinking from the intermediate state, which is to think how transfer, and then to think about how to define the state.

Published 442 original articles · won praise 330 · Views 1.23 million +

Guess you like

Origin blog.csdn.net/lw_power/article/details/103818533