• 代码随想录训练营day49, 买卖股票的最佳时机I/II


    买卖股票的最佳时机

    这题的特点就是只能买卖股票一次: 在某一天买入然后在某一天卖掉

    贪心解法: 这个比较容易理解, 就是取坐左边最小值, 然后取右边最大值, 直接拉代码

    1. class Solution {
    2. public int maxProfit(int[] prices) {
    3. int low = Integer.MAX_VALUE;
    4. int result = 0;
    5. for(int i = 0; i < prices.length; i++){
    6. //找到左边最小的
    7. low = Math.min(low, prices[i]);
    8. //直接取值
    9. result = Math.max(result, prices[i] - low);
    10. }
    11. return result;
    12. }
    13. }

    动规:

    1. 数组定义: dp[i][0]表示第i天持有股票所得最多现金(一开始现金是0, 那么持有后就是-prices[i]), dp[i][1]表示第i天不持有股票所得最多现金
    2. 确定递推公式,
      1. 如果第i天持有股票, 即dp[i][0]
        1. 如果i-1天就持有股票, 那么维持现状, 现在的现金就是昨天持有股票的所得现金(dp[i-1][0])
        2. 第i天才买入股票, 那么就是股票的现金-price[i], 比较两者最大值
      2. 如果第i天不持有股票, 即dp[i][1]
        1. 如果i-1天就不持有股票, 那么维持现状, 就是昨天没股票的现金(dp[i-1][1])
        2. 如果第i天卖掉股票, 按照股票价格+之前只有股票的情况(prices[i] + dp[i-1][0])
    3. 初始化:可以看出来基础都是从dp[i][0]何dp[i][1]推出 , dp[0][0]表示第0天就持有股票了, 所以dp[0][0] -= prices[0], dp[0][1]不持有股票就肯定是0
    4. 从前向后遍历
    5. 最后的dp[len - 1][1]就是结果, 因为不持有股票的情况肯定比持有时有钱
    1. class Solution {
    2. public int maxProfit(int[] prices) {
    3. int len = prices.length;
    4. if(prices == null || len == 0){
    5. return 0;
    6. }
    7. //创建一个二维dp数组
    8. int[][] dp = new int[len + 1][2];
    9. //初始化
    10. dp[0][0] -= prices[0];
    11. dp[0][1] = 0;
    12. //开始遍历
    13. for(int i = 1; i < len; i++){
    14. //持有股票的情况
    15. dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
    16. //不持有股票的情况
    17. dp[i][1] = Math.max(dp[i - 1][1], prices[i] + dp[i - 1][0]);
    18. }
    19. return dp[len - 1][1];
    20. }
    21. }

    也可以简化为一维数组版本

    1. class Solution {
    2. public int maxProfit(int[] prices) {
    3. int len = prices.length;
    4. if(len == 0 || prices == null){
    5. return 0;
    6. }
    7. int[] dp = new int[2];
    8. //一样进行初始化
    9. dp[0] -= prices[0];
    10. dp[1] = 0;
    11. //开始遍历, 这里往后推了一天
    12. for(int i = 1; i <= len; i++){
    13. //有股票: 之前就有/今天买的
    14. dp[0] = Math.max(dp[0], -prices[i - 1]);
    15. //无股票: 本来就没有, 和今天卖掉的
    16. dp[1] = Math.max(dp[1], prices[i - 1] + dp[0]);
    17. }
    18. return dp[1];
    19. }
    20. }

    买卖股票的最佳时机II

    上一题是只能买卖一次, 这里是可以无限买卖

    贪心算法:

    之前已经提过, 就是相当于每天都交易, 然后只要交易结果是正数就加起来

    1. class Solution {
    2. public int maxProfit(int[] prices) {
    3. int res = 0;
    4. for(int i = 1; i < prices.length; i++){
    5. res += Math.max(prices[i] - prices[i-1], 0);
    6. }
    7. return res;
    8. }
    9. }

    动态规划:

    这里的dp数组定义和上一题是一模一样的, 唯一不同的地方,就是推导dp[i][0]的时候,第i天买入股票的情况

    第i天持有股票, 要么是之前就有, 要么是第i天才买, 对于后者: 需要用之前已经有的现金-第i天的price

    第i天不持有股票, 要么是之前就无, 要么是第i天才卖掉, 对于后者: 也是今天价格+之前有的情况

    1. class Solution {
    2. public int maxProfit(int[] prices) {
    3. int len = prices.length;
    4. if(len == 0 || prices == null){
    5. return 0;
    6. }
    7. int[][] dp = new int[len + 1][2];
    8. dp[0][0] -= prices[0];
    9. dp[0][1] = 0;
    10. //开始遍历
    11. for(int i = 1; i < len; i++){
    12. //持有股票---之前就有/今天买的(要加上之前的钱)
    13. dp[i][0] = Math.max(dp[i - 1][0], -prices[i] + dp[i - 1][1]);
    14. //没有股票----之前就没/今天卖掉的
    15. dp[i][1] = Math.max(dp[i - 1][1], prices[i] + dp[i - 1][0]);
    16. }
    17. return dp[len - 1][1];
    18. }
    19. }

    优化版, 记住两点:

    1. 全部变成一维数组里, 所以直接把第一个中括号里的东西全部砍掉
    2. 因为这里往后推了一天, 这里的遍历要≤len; 同时price的价格要-1才是对的
    1. class Solution {
    2. public int maxProfit(int[] prices) {
    3. int len = prices.length;
    4. if(len == 0 || prices == null){
    5. return 0;
    6. }
    7. int[] dp = new int[2];
    8. dp[0] -= prices[0];
    9. dp[1] = 0;
    10. //开始遍历
    11. for(int i = 1; i <= len; i++){
    12. //持有股票---之前就有/今天买的(要加上之前的钱)
    13. dp[0] = Math.max(dp[0], -prices[i - 1] + dp[1]);
    14. //没有股票----之前就没/今天卖掉的
    15. dp[1] = Math.max(dp[1], prices[i - 1] + dp[0]);
    16. }
    17. return dp[1];
    18. }
    19. }

  • 相关阅读:
    【linux】coredump问题排查
    浅谈Android输入法(IME)架构
    Python查找指定元素的索引(bool索引)
    预估市场过万亿,“即时零售”到底是什么来头?
    java计算机毕业设计社区健康信息管理系统源码+系统+mysql数据库+lw文档
    动手学深度学习(pytorch版)第二章-2.2数据预处理Note-pandas
    开源是什么意思?开源软件优缺点有哪些?
    Qt信号与槽
    渗透测试流程之DVWA漏洞靶场部署
    Mysql安装
  • 原文地址:https://blog.csdn.net/Southside3amurai/article/details/128108271