๐ŸŒ— 1035. ไธ็›ธไบค็š„็บฟ

ๅžไฝ›็ซฅๅญ2022ๅนด6ๆœˆ9ๆ—ฅๅฐไบŽ 1 ๅˆ†้’Ÿ

๐ŸŒ— 1035. ไธ็›ธไบค็š„็บฟ

้šพๅบฆ: ๐ŸŒ—

้—ฎ้ข˜ๆ่ฟฐ

img_16.png


่งฃๆณ•

class Solution {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        // ๆ€่ทฏ๏ผš
        // dp[i][j] = dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1
        int rows = nums1.length;
        int cols = nums2.length;
        int[][] dp = new int[rows + 1][cols + 1];
        // ๅˆๅง‹ๅŒ–
        int res = 0;
        // dp
        for(int i = 1; i <= rows; i ++) {
            for(int j = 1; j <= cols; j ++) {
                int m = i - 1;
                int n = j - 1;
                if(nums1[m] == nums2[n]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
                res = Math.max(res, dp[i][j]);
            }
        }
        return res;
    }
}

่พ“ๅ‡บ

img_15.png

ไธŠๆฌก็ผ–่พ‘ไบŽ: 2022/6/20 ไธ‹ๅˆ8:24:47
่ดก็Œฎ่€…: liuxianzhishou