labuladong刷题——第二章(2)子序列类型问题

其实本来是不想写的,但是俄罗斯信封有一些细节可以注意一下,就写了。

1. 最长递增子序列 LC300

给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。

子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。

无非就是一个dp,记录每个位置上最长递增子序列的长度,遍历。

import java.util.Arrays;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);

        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if(nums[j]<nums[i]){
                    dp[i] = Math.max(dp[i],dp[j]+1);
                }
            }
        }
        int res = 0;
        for (int d : dp){
            res = Math.max(d, res);
        }
        return res;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

2. 俄罗斯信封问题

 先对宽度 w 进行升序排序,如果遇到 w 相同的情况,则按照高度 h 降序排序;之后把所有的 h 作为一个数组,在这个数组上计算 LIS 的长度就是答案

class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if (envelopes.length == 0) {
            return 0;
        }

        int n = envelopes.length;
        Arrays.sort(envelopes, new Comparator<int[]>() {
            public int compare(int[] e1, int[] e2) {
                if (e1[0] != e2[0]) {
                    return e1[0] - e2[0];
                } else {
                    return e2[1] - e1[1];
                }
            }
        });

        int[] f = new int[n];
        Arrays.fill(f, 1);
        int ans = 1;
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                if (envelopes[j][1] < envelopes[i][1]) {
                    f[i] = Math.max(f[i], f[j] + 1);
                }
            }
            ans = Math.max(ans, f[i]);
        }
        return ans;
    }
}


3. 最大子数组 LC53

给你输入一个整数数组 nums,请你找在其中找一个和最大的子数组,返回这个子数组的和。

还是如何找dp的问题:注意,这里可以将dp设置为以nums[i]为结尾的最大子数组之和——并且一定要注意的是,这个dp[i]必然包括了nums[i]。

其实这也很好理解,不管怎么算,最大子数组肯定包括了某个i。

在计算的过程中不要考虑前面的子序列,因为dp[i-1]也肯定以nums[i-1]结尾!


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int maxSubArray(int[] nums) {
        int[] dp = new int[nums.length];
        //注意 这个dp是以nums[i]结尾的 最大子数组之和
        // 注意 dp是包括nums[i]这个数的!就是无论如何也要带上这个数!
        dp[0] = nums[0];
        int res = Integer.MIN_VALUE;
        for (int i = 1; i < nums.length; i++) {
            //这里只有两种情况:前面是负的,不带前面
            //前面是正的,带上前面
            //不要考虑子序列问题,因为前面的dp[i-1]也包括了nums[i-1]!
            dp[i] = Math.max(dp[i-1]+nums[i],nums[i]);
            res = Math.max(res,dp[i]);
        }
        return res;
    }
}
//leetcode submit region end(Prohibit modification and deletion)


4. 最长公共子序列

给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。

一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。

 例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。

两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。

解决方案:做一个二维dp,想明白怎么转移的!


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        char[] ch1 = text1.toCharArray();
        char[] ch2 = text2.toCharArray();

        int m = text1.length();
        int n = text2.length();

        int[][] dp = new int[m+1][n+1];

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (ch1[i-1] == ch2[j-1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    //相当于多了一个位置
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }

            }
        }

        return dp[ch1.length][ch2.length];
    }
}
//leetcode submit region end(Prohibit modification and deletion)

5. 典中典之编辑距离

 将dp数组定义为,从字符串ch1的0-i位置变成字符串ch2的0-j位置所需要的变化步数。

这样,最右下角的就是我们要求的值。如图所示:

 其转移方程为:

①S1的第i个位置的字符与S2的第j个位置的字符相同:意味着该位置不用替换,只需要跟之前的一样即可——也就是dp[i-1][j-1]

②字符不同:意味着该位置需要替换或者增删,比如rad想变成rade必须+1,或者rad变成ra必须-1,这样dp[i][j]=min(dp[i-1][j]+1,dp[j-1][i]+1)

如图所示:

 


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        int[][] dp = new int[m+1][n+1];//细节1 保留一个空位

        for (int i = 0; i < dp.length; i++) {
            dp[i][0] = i;
        }
        for (int j = 0; j < dp[0].length; j++) {
            dp[0][j] = j;
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) { //细节2 小于等于m
                if(word1.charAt(i-1)==word2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]+1);
                }
            }
        }
        return dp[m][n];
    }
}
//leetcode submit region end(Prohibit modification and deletion)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值