代码随想三刷动态规划篇7

代码随想三刷动态规划篇7

  • [198. 打家劫舍](#198. 打家劫舍)
  • [213. 打家劫舍 II](#213. 打家劫舍 II)
  • [337. 打家劫舍 III](#337. 打家劫舍 III)
  • [121. 买卖股票的最佳时机](#121. 买卖股票的最佳时机)

198. 打家劫舍

题目

链接

代码

java 复制代码
class Solution {
    public int rob(int[] nums) {
        if(nums.length==1){
            return nums[0];
        }
        if(nums.length==2){
            return Math.max(nums[0],nums[1]);
        }
        
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0],nums[1]);
        for(int i=2;i<nums.length;i++){
            dp[i]= Math.max(dp[i-1],dp[i-2]+nums[i]);
        }
        return dp[nums.length-1];
    }
}

213. 打家劫舍 II

题目

链接

代码

java 复制代码
class Solution {
    public int rob(int[] nums) {
        if(nums.length==1){
            return nums[0];
        }
        if(nums.length==2){
            return Math.max(nums[0],nums[1]);
        }
        int[] dpLeft = new int[nums.length];//不偷最后一个
        int[] dpRight = new int[nums.length];//不偷第一个
        dpLeft[0] = nums[0];
        dpLeft[1] = Math.max(nums[0],nums[1]);

        dpRight[1] = nums[1];
        if(nums.length>=3){
            dpRight[2] = Math.max(nums[1],nums[2]);
        }
        for(int i =2;i<nums.length-1;i++){//不偷最后一个
            dpLeft[i] = Math.max(dpLeft[i-1],dpLeft[i-2]+nums[i]);
        }
        for(int i =3;i<nums.length;i++){//不偷前一个
            dpRight[i] = Math.max(dpRight[i-1],dpRight[i-2]+nums[i]);
        }
        return Math.max(dpLeft[nums.length-2],dpRight[nums.length-1]);
    }
}

337. 打家劫舍 III

题目

链接

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    Map<TreeNode,Integer> map = new HashMap();
    public int rob(TreeNode root) {
        if(root==null){
            return 0;
        }
        if(map.containsKey(root)){
            return map.get(root);
        }
        int money = root.val;
        if(root.left!=null){
            money+=rob(root.left.left)+rob(root.left.right);
        }
        if(root.right!=null){
            money+=rob(root.right.left)+rob(root.right.right);
        }
        int res = Math.max(money,rob(root.left)+rob(root.right));
        map.put(root,res);
        return res;
    }
}

121. 买卖股票的最佳时机

题目

链接

代码

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int[][] dp = new int[prices.length][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for(int i = 1;i<prices.length;i++){
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],-prices[i]);
        }
        return dp[prices.length-1][0];
    }
}
相关推荐
故事和你911 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__1 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__2 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
王老师青少年编程3 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心
王老师青少年编程3 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:分糖果
c++·算法·贪心算法·csp·信奥赛·线性扫描贪心·分糖果
_日拱一卒3 小时前
LeetCode:2两数相加
算法·leetcode·职场和发展
py有趣3 小时前
力扣热门100题之零钱兑换
算法·leetcode
董董灿是个攻城狮3 小时前
Opus 4.7 来了,我并不建议你升级
算法
无敌昊哥战神4 小时前
【保姆级题解】力扣17. 电话号码的字母组合 (回溯算法经典入门) | Python/C/C++多语言详解
c语言·c++·python·算法·leetcode
脱氧核糖核酸__4 小时前
LeetCode热题100——238.除了自身以外数组的乘积(题目+题解+答案)
数据结构·c++·算法·leetcode