每日三题-打家劫舍、打家劫舍III、最佳买卖股票时机含冷冻期

简介: 每日三题-打家劫舍、打家劫舍III、最佳买卖股票时机含冷冻期

打家劫舍


3a7f697ce1294457b6f7dc5e6df9c228.png

解法一

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


打家劫舍III


537cdd9cd951451998ea293f4c113a11.png

class Solution {
    HashMap<TreeNode,Integer> f = new HashMap<>();
    HashMap<TreeNode,Integer> g = new HashMap<>();
    public int rob(TreeNode root) {
        dfs(root);
        return Math.max(f.getOrDefault(root,0),g.getOrDefault(root,0));
    }
    public void dfs(TreeNode root){
        if(root == null) return; 
        dfs(root.left);
        dfs(root.right);
        f.put(root,root.val+g.getOrDefault(root.left, 0) + g.getOrDefault(root.right, 0));
        g.put(root, Math.max(f.getOrDefault(root.left, 0), g.getOrDefault(root.left, 0)) + Math.max(f.getOrDefault(root.right, 0), g.getOrDefault(root.right, 0)));
    }
}


最佳买卖股票时机含冷冻期


c4cc11ec6f0b4a9fb4ceb451192489c0.png

class Solution {
    public int maxProfit(int[] prices) {
        //dp[i][0] 表示当前持有股票的最大收益
        //dp[i][1] 表示没有股票处于冻结期
        //dp[i][2] 表示没有股票没有处于冷冻期
        int len = prices.length;
        int dp[][] = new int[len][3];
        dp[0][0] = -prices[0];
        for(int i = 1;i < len;i++){
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][2]-prices[i]);
            dp[i][1] = dp[i-1][0]+prices[i];
            dp[i][2] = Math.max(dp[i-1][1],dp[i-1][2]);
        }
        return Math.max(dp[len-1][1],dp[len-1][2]);
    }
}
class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        //dp[i][0] 表示这天不持有股票最大收益
        //dp[i][1] 表示这天持有股票最大收益
        int dp[][] = new int[len][2];
        dp[0][1] = -prices[0];
        for(int i = 1;i < len ;i++){
            dp[i][0] = Math.max(dp[i-1][1]+prices[i],dp[i-1][0]);
            dp[i][1] = Math.max(dp[i-1][1],(i<2?0:dp[i-2][0])-prices[i]);
        }
        return dp[len-1][0];
    }
}
相关文章
|
4天前
|
算法 索引
leetcode代码记录(买卖股票的最佳时机
leetcode代码记录(买卖股票的最佳时机
9 1
|
4天前
|
算法
leetcode代码记录(买卖股票的最佳时机 III
leetcode代码记录(买卖股票的最佳时机 III
12 5
|
4天前
leetcode代码记录(买卖股票的最佳时机 II
leetcode代码记录(买卖股票的最佳时机 II
8 1
|
4月前
代码随想录 Day43 动态规划11 LeetCode T309 买卖股票的最佳时期含冷冻期 T714买卖股票的最佳时机含手续费
代码随想录 Day43 动态规划11 LeetCode T309 买卖股票的最佳时期含冷冻期 T714买卖股票的最佳时机含手续费
29 0
|
4月前
代码随想录 Day41 动态规划09 LeetCode T121 买卖股票的最佳时机 T122 买卖股票的最佳时机II
代码随想录 Day41 动态规划09 LeetCode T121 买卖股票的最佳时机 T122 买卖股票的最佳时机II
27 0
|
9月前
|
算法
dp算法 力扣309最佳买卖股票时机含冷冻期
dp算法 力扣309最佳买卖股票时机含冷冻期
|
5月前
|
存储 算法 程序员
【算法训练-动态规划 一】【应用DP问题】零钱兑换、爬楼梯、买卖股票的最佳时机I、打家劫舍
【算法训练-动态规划 一】【应用DP问题】零钱兑换、爬楼梯、买卖股票的最佳时机I、打家劫舍
69 0
|
6月前
|
算法
代码随想录算法训练营第五十天 | LeetCode 309. 买卖股票的最佳时机含冷冻期、714. 买卖股票的最佳时机含手续费、股票系列总结
代码随想录算法训练营第五十天 | LeetCode 309. 买卖股票的最佳时机含冷冻期、714. 买卖股票的最佳时机含手续费、股票系列总结
52 1
|
11月前
|
算法
Leecode121. 买卖股票的最佳时机
Leecode121. 买卖股票的最佳时机
24 0
|
12月前
|
算法 Python
每日算法系列【LeetCode 309】最佳买卖股票时机含冷冻期
每日算法系列【LeetCode 309】最佳买卖股票时机含冷冻期