• 代码随想录训练营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. }

  • 相关阅读:
    时间序列预测:用电量预测 06 长短期记忆网络LSTM
    MS Access 教程之 如何在不覆盖标题字段的情况下将 Excel 数据导入 MS Access 现有表?
    华为云云耀云服务器L实例评测|使用clickhouse-benchmark工具对ClickHouse的性能测试
    UserAgent 解析
    iOS 开发代码规范
    KT142C-sop16语音芯片的4个IO口如何一对一触发播放_配置文件详细说明
    Docker常用命令
    论文简读 LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS
    Java进阶03 IO基础
    对于自动化测试的认知,大多数人容易理解错误的几个误区
  • 原文地址:https://blog.csdn.net/Southside3amurai/article/details/128108271