买卖股票的最佳时机(从简到难)

1、买卖股票的最佳时机

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

思路

  • 首先找到当前最小值
  • 用当前金额-最小值
class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if(len <= 1){
            return 0;
        }
        int max = 0;
        int min_pos = prices[0];
        for(int i=1;i<len;i++){
            if(prices[i]<prices[i-1]){
                min_pos = Math.min(min_pos,prices[i]);//获取当前最小值
            }
            max = Math.max(max,prices[i]-min_pos);//用当前金额-最小值
        }
        return max;
    }
}

2、买卖股票的最佳时机 II

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。

思路

单调栈 alt

class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if(len <= 1){
            return 0;
        }
        int res = 0;
        for(int i=1;i<len;i++){
            if(prices[i] > prices[i-1]){
                res += prices[i] - prices[i-1];//求递增序列之和
            }
        }
        return res;
    }
}

3、买卖股票的最佳时机 III

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

思路与算法

动态规划 由于我们最多可以完成两笔交易,因此在任意一天结束之后,我们会处于以下五个状态中的一种:

  • 0.没有操作
  • 1.第一次买入
  • 2.第一次卖出
  • 3.第二次买入
  • 4.第二次卖出
  • dp[i][j]中 i 表示第 i 天,j 为 [0 - 4] 五个状态,dp[i][j] 表示第 i 天状态 j 所得最大现金。
    - dp[i][1],表示的是第i天,买⼊股票的状态(持有股票后剩余的金额),并不是说⼀定要第i天买⼊ 初始化
  • 达到dp[i][1]状态,有两个具体操作:
    • 操作⼀:第i天买⼊股票了,前一天没有操作-当前金额,那么dp[i][1] = dp[i-1][0] - prices[i]
    • 操作⼆:第i天没有操作,⽽是沿⽤前⼀天买⼊的状态,即:dp[i][1] = dp[i - 1][1]
    • 所以 dp[i][1] = max(dp[i-1][0] - prices[i], dp[i - 1][1]);
  • 同理可推出剩下状态部分:
class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if(len == 0 || len == 1){
            return 0;
        }
        // dp[i][j] = 第i天买入,j天卖出的价格
        int[][] dp = new int[len][5];
        dp[0][0] = dp[0][2] = dp[0][4] = 0;
        dp[0][1] = dp[0][3] = -prices[0];
        for(int i=1;i<len;i++){
            dp[i][0] = dp[i - 1][0];
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
            dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
            dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
        }
        return dp[len-1][4];
    }
}

4、买卖股票的最佳时机 IV

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/
给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

思路与算法

同3

class Solution {
    public int maxProfit(int k, int[] prices) {
        int len = prices.length;
        if(len == 0 || len == 1){
            return 0;
        }
        // dp[i][j] = 第i天买入,j天卖出的价格
        int[][] dp = new int[len][2*k+1];
        for(int i=0;i<=2*k;i++){//奇数
            if((i & 1) == 1){
                dp[0][i] = -prices[0];
            }
        }
        for(int i=1;i<len;i++){
            for(int j=0;j<=2*k;j++){//奇数
                if(j == 0){
                    dp[i][0] = dp[i - 1][0];    
                }else{
                    if((j & 1) == 1){
                        dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j-1] - prices[i]);    
                    }else{
                        dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j-1] + prices[i]);
                    }
                }
            }            
        }
        return dp[len-1][2*k];
    }
}

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

给定一个整数数组prices,其中第  prices[i] 表示第 i 天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

思路与算法

同3 由于我们最多可以完成两笔交易,因此在任意一天结束之后,我们会处于以下五个状态中的一种:

  • 0.没有操作
  • 1.买入(持有股票)
  • 2.卖出
  • 3.冷冻期
class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if(len == 0 || len == 1){
            return 0;
        }
        // dp[i][j] = 第i天买入,j天卖出的价格
        int[][] dp = new int[len][4];
        dp[0][1] = -prices[0];
        int max = 0;
        for(int i=1;i<len;i++){
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2]);// 没有操作,可以是一直没买,或者是冷冻期之后一直没买,所以要比较卖出的利润
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
            //dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);//卖出,加了冷冻期,就不能和之前卖出数据相比较,只能是前一天持有股票状态+当前金额
            dp[i][2] = dp[i - 1][1] + prices[i];
            dp[i][3] = dp[i - 1][2];
            max = Math.max(max,dp[i][2]);
        }
        return max;
    }
}

// 精简版
class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length == 0) {
            return 0;
        }

        int n = prices.length;
        // f[i][0]: 手上持有股票的最大收益
        // f[i][1]: 手上不持有股票,并且处于冷冻期中的累计最大收益
        // f[i][2]: 手上不持有股票,并且不在冷冻期中的累计最大收益
        int[][] f = new int[n][3];
        f[0][0] = -prices[0];
        for (int i = 1; i < n; ++i) {
            f[i][0] = Math.max(f[i - 1][0], f[i - 1][2] - prices[i]);
            f[i][1] = f[i - 1][0] + prices[i];
            f[i][2] = Math.max(f[i - 1][1], f[i - 1][2]);
        }
        return Math.max(f[n - 1][1], f[n - 1][2]);
    }
}

6、买卖股票的最佳时机含手续费

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。

思路与算法

同3

  • dp[i][0] 表示第 i 天不持有可获得的最大利润;
  • dp[i][1] 表示第 i 天持有可获得的最大利润(注意是第 i 天持有,而不是第 i 天买入)。
    定义状态转移方程:
  • 不持有:dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
    • 对于今天不持有,可以从两个状态转移过来:1. 昨天也不持有;2. 昨天持有,今天卖出。两者取较大值。
  • 持有:dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
    • 对于今天持有,可以从两个状态转移过来:1. 昨天也持有;2. 昨天不持有,今天买入。两者取较大值。
class Solution {
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int[][] dp = new int[n][2]; // dp[i][1] 表示第 i 天交易完后手里持有一支股票的最大利润(i 从 0 开始)。
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < n; ++i) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[n - 1][0];
    }
}

7、剑指 Offer 63. 股票的最大利润(双指针)

https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof/
假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?

class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length<2){
            return 0;
        }
        int max=0;
        int length=prices.length;
        int left=0,right=1;
        while(right<length){
            if(prices[left]<prices[right]){
                max=Math.max(max,prices[right]-prices[left]);
            }else{
                left=right;
            }
            right++;
        }
        return max;
    }
}
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务