力扣解题思路:股票问题

本文探讨了力扣中涉及股票交易的问题,包括最佳买卖股票时机II、含冷冻期和手续费的情况。通过动态规划方法解析了不同状态的转换,提出优化方案减少状态数量,提高代码可读性。同时,介绍了在不同交易限制下的初始化和状态转移方程,强调了空间优化的重要性。
摘要由CSDN通过智能技术生成

121. 买卖股票的最佳时机


思路:这题为简单题,只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。

class Solution {
       
	public int maxProfit(int[] prices) {
           
	    if(prices.length == 0) return 0;        
	    int max = 0;        
	    int min = prices[0];        
	    for(int i=1;i<prices.length;i++){
               
		if(min>prices[i]){
                  
			min = prices[i];                
			continue;            
		}             
		max = Math.max(max,prices[i]-min);
	    }        
	    return max;    
	}
}

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


思路:为上一题的简单扩展,可以多次买卖,可以联想到贪心策略。当prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,这样的贪心策略是否是正确的呢?->
可以通过简单的证明:对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,这样的累计收益是最大的。
代码如下:

public int maxProfit(int[] prices) {
   
    int profit = 0;
    for (int i = 1; i < prices.length; i++) {
   
        if (prices[i] > prices[i - 1]) {
   
            profit += (prices[i] - prices[i - 1]);
        }
    }
    return profit;
}

309. 最佳买卖股票时机含冷冻期


思路:前两题我认为和动态规划关系不大,接下来就进入动态规划的正轨了,首先按照题目所描述的要求,我们可以画出状态转移图:
状态转移图

首先有一点需要明确的是,s2和sell这两个状态是不持有股票的,而另外两个状态是持有股票的,按照这个状态转移图,我们定义四个动态规划数组,按照状态转移的方向更新动态方程即可。
注意,在状态的初始化时,若开始不持有股票则:

sell[0] = s2[0] = 0;

若开始就持有股票则一定是当天买入的,此时:

s1[0] = buy[0] = -prices[0];

完整代码:

public int maxProfit(int[] prices) {
   
    if (prices == null || prices.length == 0) {
   
        return 0;
    }
    int N = prices.length;
    int[] buy = new int[N];
    int[] s1 = new int[N];
    int[] sell = new int[N];
    int[] s2 = new int[N];
    s1[0] = buy[0] = -prices[0];//不可以写Integer.MIN_VALUE,因为0不会再被更新而是从1开始更新,所以不可以参照空间优化的方式将其设置为Integer.MIN_VALUE
    sell[0] = s2[0] = 0;
    for (int i = 1; i < N; i++) {
   
        buy[i] = s2[i - 1] - prices[i];
        s1[i] = Math.max(buy[i - 1], s1[i - 1]);
        sell[i] = Math.max(buy[i - 1], s1[i - 1]) + prices[i];
        s2[i] = Math.max(s2[i - 1], sell[i - 1]);
    }
    return Math.max(sell[N - 1], s2[N - 1]);
}

同样的,类似的有

714. 买卖股票的最佳时机含手续费


思路:同样可以参考上面的状态转移图,但是图中有个地方我标错了buy的时候是不用扣除手续费的,完整的代码如下:

public int maxProfit(int[] prices, int fee) {
   
    int N = prices.length;
    int[] buy = new int[N];
    int[] s1 = new int[N];
    int[] sell = new int[N];
    int[] s2 = new int[N];
    s1[0] = buy[0] = -prices[0];//不可以写Integer.MIN_VALUE,因为0不会再被更新而是从1开始更新,所以不可以参照空间优化的方式将其设置为Integer.MIN_VALUE
    sell[0] = s2[0] = 0;
    for (int i = 1; i < N; i++) {
   
        buy[i] = Math.max(sell[i - 1], s2[i - 1]) - prices[i];
        s1[i] = Math.max(buy[i - 1], s1[i - 1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值