DP EASY 21.11.09

动态规划三要素

  1. 边界
  2. 最优子结构
  3. 状态转移函数

53. 最大子序和

53. 最大子序和

  1. 边界 f(1)=nums[0]
  2. 最优子结构 f(i-1) 和nums[i]
  3. 状态转移函数:

-21-34-121-54
Pre-21-2435615
Max-211445666

Java实现

  1. 两条数组,空间复杂度高
class Solution {
    public int maxSubArray(int[] nums) {
        int len = nums.length; 
        int[] subMax = new int[len];
        int[] ans = new int[len];
        subMax[0] = nums[0];
        ans[0] = nums[0];
        for (int i = 1; i < len; i++){
            if (subMax[i - 1] + nums[i] > nums[i]){
                subMax[i] = subMax[i - 1] + nums[i];
            }else {
                subMax[i] = nums[i];
            }
            if (subMax[i] > ans[i - 1]){
                ans[i] = subMax[i];
            }else {
                ans[i] = ans[i - 1];
            }
        }
        return ans[len - 1];
    }
}

执行用时:3 ms, 在所有 Java 提交中击败了12.48%的用户

内存消耗:54.4 MB, 在所有 Java 提交中击败了5.02%的用户


  1. 两个变量
class Solution {
    public int maxSubArray(int[] nums) {
        int pre = nums[0], ans = nums[0];
        for (int i = 1; i < nums.length; i++){
            pre = Math.max(pre + nums[i], nums[i]);
            ans = Math.max(ans, pre);
        }
        return ans;
    }
}

执行用时:1 ms, 在所有 Java 提交中击败了94.15%的用户

内存消耗:48.5 MB, 在所有 Java 提交中击败了25.27%的用户


新增知识:

//求最大值
x = Math.max(a, b);
// x= Math.min(a, b);
//同类型变量声明及赋值可以用逗号隔开
int x = 3, y = 5;

392. 判断子序列

392. 判断子序列

Java实现


1、双指针

class Solution {
    public boolean isSubsequence(String s, String t) {
        int j = 0, slen = s.length(), tlen = t.length(), k = 0;
        for (int i = 0; i < slen; i++){
            while (j < tlen){
                if (s.charAt(i) != t.charAt(j)){
                    j++;
                }else {
                    j++;
                    k++;
                    break;
                }
            }
        }
        if (k != slen){
            return false;
        }else {
            return true;
        }
    }
}

改进:

class Solution {
    public boolean isSubsequence(String s, String t) {
        int j = 0, slen = s.length(), tlen = t.length(), k = 0;
        while (j < slen && k < tlen){
            if (s.charAt(j) == t.charAt(k)){
                j++;
            }
            k++;
        }
        return j == slen;
    }
}

2、动态规划

???脑阔疼


746. 使用最小花费爬楼梯

746. 使用最小花费爬楼梯

  1. 边界:在0,1台阶时花费为0
  2. 最优子结构
  3. 状态转移方程:当前花费 等于 前两个台阶的花费 加上 本身的花费 的最小值
台阶1100111100111001
costSum00122334456
台阶1015202
costSum00101517

Java实现

  1. 数组
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int len = cost.length;
        int[] ans = new int[len + 1];
        ans[0] = 0; 
        ans[1] = 0;
        for (int i = 2; i <= len; i++){
            ans[i] = Math.min(ans[i-2] + cost[i-2], ans[i-1] + cost[i-1]);
        }
        return ans[len];
    }
}

执行用时:1 ms, 在所有 Java 提交中击败了92.05%的用户

内存消耗:38 MB, 在所有 Java 提交中击败了73.31%的用户


  1. 滚动数组
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int ans = 0, pre = 0, temp = 0;
        for (int i = 2; i <= cost.length; i++){
            temp = ans;
            ans = Math.min(ans + cost[i-1], pre + cost[i-2]);
            pre = temp;
        }
        return ans;
    }
}

执行用时:1 ms, 在所有 Java 提交中击败了92.05%的用户

内存消耗:37.9 MB, 在所有 Java 提交中击败了90.69%的用户


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值