121、买股票的最佳时机

买卖股票的最佳时机,第一问

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。

注意你不能在买入股票前卖出股票。

示例 1:

输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。

示例 2:

输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

思路

  • 动态规划的最简单思路——辅助变量存储临时状态
    • 最大利润 max
    • 最小股票价格 minPrice
    • 遍历一遍,动态更新最优的结果

代码

class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) return 0;
        int max = 0;
        int minPrice = prices[0];
        for (int i = 1;i < prices.length;i++) {
            if (minPrice > prices[i]) {
                minPrice = prices[i];
            }
            int profit = prices[i] - minPrice;
            max = max < profit? profit:max; 
        }
        return max;
    }
}

变相问题——求获得最大利润的两个买入和卖出的下标

  • 利润为0时,输出[0,0]
class Solution {
    public int[] maxProfitDate(int[] prices) {
        int[] res = new int[2];
        if (prices == null || prices.length <= 1) return res;
        res[0] = 0;// minIndex
        res[1] = 0;// maxPrifitIndex
        for (int i = 1;i < prices.length;i++) {
            if (prices[res[0]] > prices[i]) {
                res[0] = i;
            }
            int profit = prices[i] - prices[res[0]];
            int max = prices[res[1]] - prices[res[0]]
            res[1] = max < profit? i : res[1]; 
        }
        return res;
    }
}

买卖股票的最佳时机,第二问

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易 (多次买卖一支股票)

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入: [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。

示例 2:

输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:

输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

思路

  1. 只统计连续的谷和峰的差值
  2. 统计连续递增区间的增量

代码

// 1
public int maxProfit(int[] prices) {
    if (prices == null || prices.length == 0) return 0;
    int i = 0;
    int valley = prices[0];
    int peek = prices[0];
    int maxProfit = 0;
    while(i < prices.length - 1) {
        // 递减求波谷
        while (i < prices.length - 1 && prices[i] >= prices[i + 1]) {
            i++;
        }
        valley = prices[i];
        // 递增求波峰
        while (i < prices.length - 1 && prices[i] <= prices[i + 1]) {
            i++;
        }
        // 计算利润
        peek = prices[i];
        maxProfit += peek - valley;
        // continue 求下一个波谷和波峰
    }
    return maxProfit;
}
// 2 
public int maxProfit(int[] prices) {
    int profit = 0;
    for (int i = 1;i < prices.length;i++) {
        int tmp = prices[i] - prices[i-1];
        // 第i天涨价,就是应得的利润
        profit += tmp > 0 ? tmp : 0;
    }
    return profit;
}

public maxProfit_k_inf(int[] prices) {
    int n = prices.length;
    int dp_i_0 = 0,dp_i_1 = Interger.MIN_VALUE;
    for (int i = 0;i < n;i++) {
        int temp = dp_i_0;
        dp_i_0 = Math.max(dp_i_0,dp_i_1 + prices[i]);
        dp_i_1 = Math.max(dp_i_1,temp - prices[i]);
    }
    return dp_i_0;
}

123、买卖股票的最佳时机,第三问

给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入: [3,3,5,0,0,3,1,4]
输出: 6
解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
     随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。

示例 2:

输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。   
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。   
     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:

输入: [7,6,4,3,1] 
输出: 0 
解释: 在这个情况下, 没有交易完成, 所以最大利润为 0。

思路——动态规划

  • DP table

代码

// 常规dp数组
public int maxProfit(int[] prices) {
    int max_k = 2;
    int n = prices.length;
    int[][][] dp = new int[n][max_k + 1][2];
    for (int i = 0;i < n;i++) {// 遍历每一天
        for (int k = max_k;k >= 1;k--) {// 遍历每一个次的股票买卖,更新大值
            if (i - 1 == -1) {
                dp[i][k][0] = 0;
                dp[i][k][1] = Integer.MIN_VALUE;
                continue;
            }
            dp[i][k][0] = Math.max(dp[i-1][k][0],dp[i-1][k][1] + prices[i]);
            dp[i][k][1] = Math.max(dp[i-1][k][1],dp[i - 1][k - 1][0] - prices[i]);
        }
    }
    return dp[n - 1][max_k][0];
}

// 空间优化
public int macProfit(int[] prices) {
    // 第一次的购买抛出的利润和买入后的本金
    int dp_i10 = 0,dp_i11 = Integer.MIN_VALUE;
    // 第二次的购买抛出的利润和买入后的本金
    int dp_i20 = 1,dp_i21 = Integer.MIN_VALUE;
    for (int price: prices) {
        // 更新第二次抛出
        dp_i20 = Math.max(dp_i20,dp_i21 + price);\
        // 更新第二次买入
        dp_i21 = Math.max(dp_i21,dp_i10 - price);
        // 更新第一次抛出
        dp_i10 = Math.max(dp_i10,dp_i11 + price);
        // 更新第一次抛出
        dp_i11 = Math.max(dp_i11, - price);
    }
    return dp_i20;
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:121、买股票的最佳时机

Count:1.6k

Author:攀登

Created At:2020-07-26, 00:19:44

Updated At:2024-06-15, 15:52:32

Url:http://jiafeimao-gjf.github.io/2020/07/26/121%E3%80%81%E4%B9%B0%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation