LeetCode炒股系列——买卖股票的最佳时机

今天我们总结一下力扣题库中的有关买卖股票的最佳时机的题目:

  1、买卖股票的最佳时机

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

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

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

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

  6、买卖股票的最佳时机含冻结费

  这类题目很显然是使用动态规划解题,动态规划点此跳转

1、买卖股票的最佳时机

题目:

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

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

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

  示例 1:

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

解题:

  我们定义dp[i]表示前i天获得的收益,minPrice表示前i天股票的最小值,那么转移方程如下:  

  minPrice=min(minPrice, prices[i]);

  dp[i]=max(dp[i-1], prices[i]-min);

  初始值dp[0]=0;

代码如下:

public static int maxProfit(int[] prices) {
    int n = prices.length;
    if (n == 0) return 0;
    int[] dp = new int[n];

    int min = prices[0];
    for (int i = 1; i < n; i++) {
        min = Math.min(min, prices[i]);
        dp[i] = Math.max(dp[i - 1], prices[i] - min);
    }

    return dp[n - 1];
}

空间优化

我们可以看到最后只需要最优值,所以我们只需要维护一个最优收益变量即可。

代码:

public static int maxProfit1(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int max = 0;
        int min = prices[0];
        for (int i = 1; i < n; i++) {
            min = Math.min(min, prices[i]);
            max = Math.max(max, prices[i] - min);
        }

        return max;
    }

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

题目:

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

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

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

 示例 1:

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

解题:

  我们定义buy[i]表示前i天获得的收益(手中还买了股票),sell[i]表示前i天的收益(手中已经没有了股票),那么转移方程如下:

  buy[i]=max(buy[i-1],sell[i-1]-prices[i]);

  sell[i]=max(sell[i-1],buy[i-1]+prices[i]);  

  初始值buy[0]=-prices[i], sell[0]=0

代码如下:

public static int maxProfit(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int[] buy = new int[n];
        int[] sell = new int[n];
        buy[0] = -prices[0];
        sell[0] = 0;

        for (int i = 1; i < n; i++) {
            buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]);
            sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);
        }

        return sell[n - 1];
    }

空间优化:

由于当前状态只和前一状态相关,所以我们只需要维护一个变量即可。

public static int maxProfit1(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int buy = -prices[0];
        int sell = 0;

        for (int i = 1; i < n; i++) {
            buy = Math.max(buy, sell - prices[i]);
            sell = Math.max(sell, buy + prices[i]);
        }

        return sell;
    }

贪心算法

  当然这个题目可以使用贪心的思想解决。

  由于股票的购买没有限制,因此整个问题等价于寻找 x 个不相交的区间 (li,ri] 使得如下的等式最大化:

 

 代码

public int maxProfit(int[] prices) {
        int ans = 0;
        int n = prices.length;
        for (int i = 1; i < n; ++i) {
            ans += Math.max(0, prices[i] - prices[i - 1]);
        }
        return ans;
    }

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

题目:

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

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

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

  示例 1:

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

解题:

  我们定义:

  dp[i][0]:第一次购买,初始为-prices[0]

  dp[i][1]:第一次卖出,初始为0

  dp[i][2]:第二次购买,初始为-prices[0]

  dp[i][3]:第二次卖出,初始为0

  则转移方程为:

  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]);

  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]);

代码如下:

public static int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][4];

dp[0][0] = -prices[0];
dp[0][1] = 0;
dp[0][2] = -prices[0];
dp[0][3] = 0;
for (int i = 1; i < n; 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]);
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]);
}

return Math.max(dp[n-1][1],dp[n-1][3]);
}

空间优化:

由于当前状态只和前一状态相关,所以我们只需要维护一个变量即可。

public static int maxProfit(int[] prices) {
int n = prices.length;
if(n==0) return 0;
int dp0 = -prices[0];
int dp1 = 0;
int dp2 = -prices[0];
int dp3 = 0;
for (int i = 1; i < n; i++) {
dp0 = Math.max(dp0, -prices[i]);
dp1 = Math.max(dp1, dp0 + prices[i]);
dp2 = Math.max(dp2, dp1 - prices[i]);
dp3 = Math.max(dp3, dp2 + prices[i]);
}

return Math.max(dp1, dp3);
}

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

题目:

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

示例 1:

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

解题:

  我们定义buy[i]j][表示进行恰好 笔交易,并且当前手上持有一支股票,这种情况下的最大利润,sell[i][j]表示恰好进行 j 笔交易,并且当前手上不持有股票,这种情况下的最大利润,那么转移方程为:

  buy[i][j]=max(buy[i-1][j], sell[i-1][j]-prices[i])

  sell[i][j]=max(sell[i-1][j], buy[i-1][j-1]+prices[i])

  最终的答案即为sell[n-1][0...k]中的最大值。

  初始值buy[0][0]=-prices[i], buy[1-n][1...k]设置为非常小的值,表示不合法。sell[0][0]=0,sell[1-n][1...k]设置为非常小的值,表示不合法。

  最后需要注意的是,本题中 k 的最大值可以达到 10^9,然而这是毫无意义的,因为 n 天最多只能进行[n/2​]笔交易,

代码如下:

public static int maxProfit(int k, int[] prices) {
int n=prices.length;
if(n==0) return 0;
k=Math.min(k,n/2);

int[][] buy=new int[n][k+1];
int[][] sell=new int[n][k+1];

buy[0][0]=-prices[0];
sell[0][0]=0;
for (int i = 1; i <=k; i++) {
buy[0][i]=sell[0][i]=Integer.MIN_VALUE/2;
}

for (int i = 1; i < n; i++) {
buy[i][0]=Math.max(buy[i-1][0],sell[i-1][0]-prices[i]);
for (int j = 1; j <= k; j++) {
buy[i][j]=Math.max(buy[i-1][j],sell[i-1][j]-prices[i]);
sell[i][j]=Math.max(sell[i-1][j],buy[i-1][j-1]+prices[i]);
}
}

return Arrays.stream(sell[n-1]).max().getAsInt();
}

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

题目:

  给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。返回获得利润的最大值。

  注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。

示例 1:

输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润: 
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

解题:

  此题和买卖股票的最佳时机II非常相似,只是多了一个手续费。

  我们定义buy[i]表示前i天获得的收益(手中还买了股票),sell[i]表示前i天的收益(手中已经没有了股票),那么转移方程如下:

  buy[i]=max(buy[i-1],sell[i-1]-prices[i]);

  sell[i]=max(sell[i-1],buy[i-1]+prices[i]-fee);  

  初始值buy[0]=-prices[i], sell[0]=0

代码如下:

public static int maxProfit(int[] prices,int fee) {
int n = prices.length;
if (n == 0) return 0;
int[] buy = new int[n];
int[] sell = new int[n];
buy[0] = -prices[0];
sell[0] = 0;

for (int i = 1; i < n; i++) {
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]);
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]-fee);
}

return sell[n - 1];
}

空间优化:

由于当前状态只和前一状态相关,所以我们只需要维护一个变量即可。

public static int maxProfit(int[] prices,int fee ) {
int n = prices.length;
if (n == 0) return 0;
int buy = -prices[0];
int sell = 0;

for (int i = 1; i < n; i++) {
buy = Math.max(buy, sell - prices[i]);
sell = Math.max(sell, buy + prices[i]-fee);
}

return sell;
}

6、买卖股票的最佳时机含冻结期

题目:

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

  示例:

  输入: [1,2,3,0,2]

  输出: 3 

  解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]

解题:

  我们定义:

  dp[i][0]:现在持有股票时的利润,初始为-prices[0]

  dp[i][1]:现在处于冻结期的利润,初始为0

  dp[i][2]:现在没有股票且不处于冻结期的利润,初始为0

  则转移方程为:

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

  dp[i][1] = dp[i - 1][0] + prices[i];

  dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]);

代码如下:

public static int maxProfit(int[] prices) {
int n = prices.length;
if(n==0) return 0;
int[][] dp = new int[n][3];

dp[0][0] = -prices[0];
dp[0][1] = 0;
dp[0][2] = 0;
for (int i = 1; i < n; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i-1][2]-prices[i]);
dp[i][1] = dp[i][0]+prices[i];
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]);
}

return Math.max(dp[n - 1][1], dp[n - 1][2]);
}

空间优化:

由于当前状态只和前一状态相关,所以我们只需要维护一个变量即可。

int n = prices.length;
if(n==0) return 0;
int dp0 = -prices[0];
int dp1 = 0;
int dp2 = 0;
for (int i = 1; i < n; i++) {
dp0 = Math.max(dp0, dp2-prices[i]);
dp2 = Math.max(dp2, dp1);
dp1 = dp0+prices[i];
}

return Math.max(dp1, dp2);

 

posted @ 2021-01-09 17:40  SupremeBoy  阅读(293)  评论(0)    收藏  举报