leetcode 买卖股票系列题目总结

总结:买卖股票系列题目

1、买卖股票的最佳时机(简单)

121. 买卖股票的最佳时机

难度简单1093

给定一个数组,它的第 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。

解答

//1、最简单的两重循环,求prices[j]-prices[i]最大值即可
class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length==0||prices==null) return 0;
        int max=0;
        for(int i=0;i<prices.length-1;i++){
            for(int j=i+1;j<prices.length;j++){
                max=Math.max(max,prices[j]-prices[i]);
            }
        }
        return max;
    }
}
//2、一重遍历即可,思路:记录最低点,然后找到后续卖出最大值
public class Solution {
    public int maxProfit(int prices[]) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice)
                minprice = prices[i];
            else if (prices[i] - minprice > maxprofit)
                maxprofit = prices[i] - minprice;
        }
        return maxprofit;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/121-mai-mai-gu-piao-de-zui-jia-shi-ji-by-leetcode-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

2、买卖股票的最佳时机2(简单)

122. 买卖股票的最佳时机 II

难度简单785

给定一个数组,它的第 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 <= prices.length <= 3 * 10 ^ 4

  • 0 <= prices[i] <= 10 ^ 4

解答

//思路:一次遍历,盈利实质是当天与前天价格差(负值就不参与买卖)
class Solution {
    public int maxProfit(int[] prices) {
        int result=0;
        for(int i=0;i<prices.length-1;i++){
            if(prices[i+1]-prices[i]>0){
                result=result+prices[i+1]-prices[i];
            }
        }
        return result;
    }
}
//官方的暴力法,很难想到且不可取
class Solution {
    public int maxProfit(int[] prices) {
        return calculate(prices, 0);
    }

    public int calculate(int prices[], int s) {
        if (s >= prices.length)
            return 0;
        int max = 0;
        for (int start = s; start < prices.length; start++) {
            int maxprofit = 0;
            for (int i = start + 1; i < prices.length; i++) {
                if (prices[start] < prices[i]) {
                    int profit = calculate(prices, i + 1) + prices[i] - prices[start];
                    if (profit > maxprofit)
                        maxprofit = profit;
                }
            }
            if (maxprofit > max)
                max = maxprofit;
        }
        return max;
    }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

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

123. 买卖股票的最佳时机 III

难度困难450

给定一个数组,它的第 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,三维数组,未优化)
解题思路

  1. 定义状态:dp一维日期,二维持有状态,0,不持有股票,持有现金,1持有股票,三维卖出次数:0 ,1,2
    dp[i][j][k] 表示:在下标为 i 的这一天,用户手上持股状态为 j 所获得的最大利润。
  2. DP方程:
  • 2.1不持股票,来源:
    1. dp[i][0][0] 未卖出过:dp[i-1][0][0]
    2. 之前已卖出一次,昨天未持有股票dp[i-1][0][1],或昨天持有股票,今天第一次将持有股票卖出dp[i-1][1][0]+prices[i];
    dp[i][0][1] = Math.max(dp[i-1][0][1],dp[i-1][1][0]+prices[i]);
    3. 之前已卖出二次,昨天未持有股票dp[i-1][0][2],或昨天持有股票,已卖出过一次,今天第二次将持有股票卖出dp[i-1][1][1]+prices[i];
    dp[i][0][2] = Math.max(dp[i-1][0][2],dp[i-1][1][1]+prices[i]);
  • 2.2 持有股票,来源:
    1. 未卖出:之前已买入,昨天持有股票dp[i-1][1][0],或昨天未持有,今天买入dp[i-1][0][0]-prices[i]
    dp[i][1][0] = Math.max(dp[i-1][1][0],dp[i-1][0][0]-prices[i]);
    2. 卖出1次:昨天持有股票,但之前已卖出一次,dp[i-1][1][1],或昨天未持有,之前已卖出一次,今天再次买入dp[i-1][0][1]-prices[i]
    dp[i][1][1] = Math.max(dp[i-1][1][1],dp[i-1][0][1]-prices[i]);
    3. 卖出二次 昨天持有股票但之前已卖出二次,dp[i-1][1][2], 但根据题意最多交易两次,因此这种情况不存在
  1. 初始状态:
    假设:
    第一持有,不管卖出次数是否存在,值都为:dp[0][1][?]=-prices[0];
    第一天不持有,不管卖出次数是否存在,值都为: dp[0][0][?]=0;
  2. 结果输出:
    获取最大收益,一定是用户手上不在持有股票,可能交易过一次,也可能交易过两次
    Math.max(dp[len-1][0][2],dp[len-1][0][1]);
    代码

class Solution {
public int maxProfit(int[] prices) {
    if(prices.length<2) return 0;
    int len =  prices.length;
    int[][][] dp = new int[len][2][3];
    dp[0][0][0] = 0;
    dp[0][0][1] = 0;
    dp[0][0][2] = 0;
    dp[0][1][0] = -prices[0];
    dp[0][1][1] = -prices[0];
    dp[0][1][2] = -prices[0];
    for(int i=1;i<len;++i){
    dp[i][0][0] = 0;
    dp[i][0][1] = Math.max(dp[i-1][0][1],dp[i-1][1][0]+prices[i]);
    dp[i][0][2] = Math.max(dp[i-1][0][2],dp[i-1][1][1]+prices[i]);
    dp[i][1][0] = Math.max(dp[i-1][1][0],dp[i-1][0][0]-prices[i]);
    dp[i][1][1] = Math.max(dp[i-1][1][1],dp[i-1][0][1]-prices[i]);
    dp[i][1][2] = 0;
    }
    return Math.max(dp[len-1][0][2],dp[len-1][0][1]);
}

}
方法二(动态规划 二维数组)
解题思路
1.状态:0,未交易,1,买入一次,2卖出1次,3买入2次,4卖出2次
dp[i][j] 用户手上金钱
2. DP公式
- 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]);
    初始值
    dp[0][0] = 0;
    dp[0][1] = -prices[0];
    结果输出
    用户收益为卖出一次,或两次后的最大利润

     ```java
     public int maxProfit(int[] prices) {
               if(prices.length<2) return 0;
             int len =  prices.length;
             int[][] dp = new int[len][5];
             dp[0][0] = 0;
             dp[0][1] = -prices[0];
             //实际状态依赖与上一状态,以下情况实际不存
             dp[0][2] = 0;
             dp[0][3] = -prices[0];
             dp[0][4] = 0;
             for(int i=1;i<len;++i){
                 dp[i][0] = 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 Math.max(dp[len-1][2],dp[len-1][4]);
         }
     ```
    

方法三 (一维数组,方法二的第一次优化)

解题思路
1.状态:0,未交易,1,买入一次,2卖出1次,3买入2次,4卖出2次
dp[]:交易后用户手上金额
2.DP公式
没做交易值永远为0
dp[0] = 0;
//买入1次,只能从之前已买,或之前没买,当天第一次买入获得
dp[1] = Math.max(dp[1],dp[0]-prices[i]);
//买出1次,只能从之前已卖出1次,或之前没卖,当天第一次卖出获得
dp[2] = Math.max(dp[2],dp[1]+prices[i]);
//买入2次,只能从之前已买2次,或之前买了一次,当天第二次卖出获得
p[3] = Math.max(dp[3],dp[2]-prices[i]);
//买出2次,只能从之前已卖出2次,或之前卖了一次,当天第二次卖出获得
dp[4] = Math.max(dp[4],dp[3]+prices[i]);
3.初始值:
第一天 没做交易:用户手上 dp[0] = 0
第一天 买入一次:用户手上 dp[1] = -prices[0]
4.输出结果:
最大收益:卖出1次或卖出两次中最大收益

代码

public int maxProfit3(int[] prices) {
    if(prices.length<2) return 0;
    int len =  prices.length;
    int[] dp = new int[5];
    dp[0] = 0;//
    dp[1] = -prices[0];
    //实际状态依赖与上一状态,以下情况实际不存
    dp[2] = Integer.MIN_VALUE;
    dp[3] = Integer.MIN_VALUE;
    dp[4] = Integer.MIN_VALUE;
    for(int i=1;i<len;++i){
        dp[0] = 0;
        dp[1] = Math.max(dp[1],dp[0]-prices[i]);
        dp[2] = Math.max(dp[2],dp[1]+prices[i]);
        dp[3] = Math.max(dp[3],dp[2]-prices[i]);
        dp[4] = Math.max(dp[4],dp[3]+prices[i]);
    }
    return Math.max(dp[2],dp[4]);
}

作者:echoes-a
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/mai-mai-gu-piao-zui-jia-shi-ji-iiidong-tai-gui-hua/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值