代码随想录 Day41 动态规划09 LeetCode T121 买卖股票的最佳时机 T122 买卖股票的最佳时机II

简介: 代码随想录 Day41 动态规划09 LeetCode T121 买卖股票的最佳时机 T122 买卖股票的最佳时机II

前言

这两题看起来是不是有点眼熟,其实我们在贪心章节就已经写过了这两道题,当时我们用的是将利润分解,使得我们始终得到的是最大利润

假如第 0 天买入,第 3 天卖出,那么利润为:prices[3] - prices[0]。

相当于(prices[3] - prices[2]) + (prices[2] - prices[1]) + (prices[1] - prices[0])。

这样就是每天得到的最大利润 ,下面我也会给出贪心的思路

LeetCode T121 买卖股票的最佳时机

题目链接:121. 买卖股票的最佳时机 - 力扣(LeetCode)

题目思路:

我们还是用动规五部曲来解决问题

1.确定动规数组含义

这里我们定义两种状态,

1.dp[i][0] 表示持有股票的状态

2.dp[i][1]表示不持有股票的状态

所以此时的dp[i][0]和dp[i][1]都是持有股票时的最大钱数和不持有的最大钱数

注:这里的持有和不持有股票不是指当天买入股票,也可能是之前延续下来的一种状态

2.确定递推公式

这里持有股票可能是前面延续下来的一种状态也可能是当时购买股票的一个状态,我们取最大值即可

dp[i][0] = Math.max(dp[i-1][0],-prices[i])

同样下面也是一样我们讨论没有持有股票的最大值

dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]+prices[i])

3.初始化dp数组

由递推公式可知只要初始化第一个即可

dp[i][0] = -prices[0]

dp[i][1] = 0

4.确定遍历方式

顺序遍历,因为后一个结果的产生取决于前一个结果

5.打印dp数组排错

题目代码:

//贪心
class Solution {
    public int maxProfit(int[] prices) {
        // 找到一个最小的购入点
        int low = Integer.MAX_VALUE;
        // res不断更新,直到数组循环完毕
        int res = 0;
        for(int i = 0; i < prices.length; i++){
            low = Math.min(prices[i], low);
            res = Math.max(prices[i] - low, res);
        }
        return res;
    }
}
//动规
class Solution {
    public int maxProfit(int[] prices) {
       if(prices.length<=1){
            return 0;
        }
        int[][] dp = new int[prices.length][2];
        dp[0][0] = -prices[0];
        dp[0][1] = 0;
        for(int i = 1;i<prices.length;i++){
            dp[i][0] = Math.max(dp[i-1][0],-prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]+prices[i]);
        }
        int result = Math.max(dp[prices.length-1][0],dp[prices.length-1][1]);
        return result;
    }
}

LeetCode T122 买卖股票的最佳时机 II

题目链接:122. 买卖股票的最佳时机 II - 力扣(LeetCode)

题目思路:

这道题和之前的区别就是买卖股票的次数不仅仅是一次了,所以我们需要将持有股票的状态修改一下,其余代码均不变

dp[i][0]  = Math.max(dp[i-1][0],dp[i-1][1]-price[i])这是因为之前只能购买一次,所以不持有股票的状态的钱数一定是0,这里就不一样了,可以购买多次.

题目代码:

//贪心
class Solution {
    public int maxProfit(int[] prices) {
        int maxP = 0;
        for(int i = 0;i<prices.length-1;i++)
        {
            maxP += Math.max(prices[i+1] - prices[i],0);
        }
        return maxP;
    }
}
//动规
class Solution {
    public int maxProfit(int[] prices) {
       if(prices.length<=1){
            return 0;
        }
        int[][] dp = new int[prices.length][2];
        dp[0][0] = -prices[0];
        dp[0][1] = 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],dp[i-1][0]+prices[i]);
        }
        int result = Math.max(dp[prices.length-1][0],dp[prices.length-1][1]);
        return result;
    }
}

 

相关文章
|
5天前
|
机器学习/深度学习
leetcode代码记录(旋转图像
leetcode代码记录(旋转图像
10 0
|
5天前
|
算法
leetcode代码记录(全排列 II
leetcode代码记录(全排列 II
13 4
|
5天前
|
算法
leetcode代码记录(全排列
leetcode代码记录(全排列
12 1
|
5天前
|
索引
leetcode代码记录(Z 字形变换
leetcode代码记录(Z 字形变换
12 1
|
5天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
5天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
12 0
|
5天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
5天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
27 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
5天前
|
存储 算法 测试技术