[一文搞定股票买卖问题]: 刷通[买卖股票]

121. 买卖一次:买卖股票的最佳时机

Difficulty: 简单

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

Solution

转化为最大子序和问题,详解戳开链接

class Solution {
    public int maxProfit(int[] prices) {
        if(prices==null||prices.length==0) return 0;
    	int[] fluc=new int[prices.length-1];
    	for(int i=1;i!=prices.length;i++) {
    		fluc[i-1]=prices[i]-prices[i-1];
    	}
    	int curMaxWithRight=0;
    	int res=0;
    	for(int j=0;j!=fluc.length;j++) {
    		curMaxWithRight=Math.max(curMaxWithRight+fluc[j],fluc[j]);
    		res=Math.max(res, curMaxWithRight);
    	}
    	return res;
    }
}

存储波底和最大收益 O(n)

我们需要找到最小的谷之后的最大的峰。
我们可以维持两个变量——minprice 和 maxprofit,它们分别对应迄今为止所得到的最小的谷值和最大的利润(卖出价格与最低价格之间的最大差值)。

class Solution {
    public int maxProfit(int[] prices) {
    	int minPrice=Integer.MAX_VALUE;//波底
    	int maxProfit=0;//最大获利
    	for(int i=0;i!=prices.length;i++) {
    		minPrice=Math.min(minPrice, prices[i]);
    		maxProfit=Math.max(maxProfit, prices[i]-minPrice);
    	}
    	return maxProfit;
    }
}

自底向上动态优化问题

O(n)空间复杂度

class Solution {
    public int maxProfit(int[] prices) {
    	//为空赚个屁
    	if(prices==null||prices.length==0) return 0;
    	//动态规划 状态转移
    	int[][]dp=new int[prices.length][2];
    	//第一天不买 获利0元 ; 第一天手里已经买了 获利第一天股票的负数
    	dp[0][0]=0;dp[0][1]=-prices[0];
    	for(int i=1;i!=prices.length;i++) {
    		//前一天也是没有买 or前一天买了但今天卖出去了
    		dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
    		//前一天时就已经买了or前一天还没买,今天才买的(本金为0)
    		dp[i][1]=Math.max(dp[i-1][1],0-prices[i]);
    	}
    	return dp[prices.length-1][0];
    }
}

O(1)空间复杂度

class Solution {
    public int maxProfit(int[] prices) {
    	//为空赚个屁
    	if(prices==null||prices.length==0) return 0;
    	//第一天不买 获利0元 ; 第一天手里已经买了 获利第一天股票的负数
    	int yesterdayYes=-prices[0];//昨天买了
    	int yesterdayNo=0;//昨天没有买
    	for(int i=1;i!=prices.length;i++) {
    		//前一天也是没有买 or前一天买了但今天卖出去了
    		yesterdayNo=Math.max(yesterdayNo,yesterdayYes+prices[i]);
    		//前一天时就已经买了or前一天还没买,今天才买的(本金为0)
    		yesterdayYes=Math.max(yesterdayYes,0-prices[i]);
    	}
    	//最终返回的是最后一天,手中没有持有股票的情况
    	return yesterdayNo;
    }
}

122. 买卖次数不限:买卖股票的最佳时机 II

Difficulty: 简单

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

Solution

动态优化自底向上

O(n)空间复杂度

class Solution {
    public int maxProfit(int[] prices) {
    	if(prices==null||prices.length==0) return 0;
    	int[][]dp=new int[prices.length][2];
    	dp[0][0]=0;dp[0][1]=-prices[0];
    	for(int i=1;i!=prices.length;i++) {
    		dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
    		//前一天时就手里有股票or前一天手里没股票,今天才买的(本金为上次累积的)
    		dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);	
    	}
    	return dp[prices.length-1][0];
    }
}

O(1)空间复杂度

class Solution {
    public int maxProfit(int[] prices) {
    	if(prices==null||prices.length==0) return 0;
    	int yesterdayNo=0,yesterdayYes=-prices[0];
    	for(int i=1;i!=prices.length;i++) {
    		//前一天也是手里没股票 or前一天手里有股票但今天卖出去了
    		yesterdayNo=Math.max(yesterdayNo,yesterdayYes+prices[i]);
    		//前一天时就手里有股票or前一天手里没股票,今天才买的(本金为上次累积的)
    		yesterdayYes=Math.max(yesterdayYes,yesterdayNo-prices[i]);
    	}
    	return yesterdayNo;
    }
}

123. 最多2笔法:买卖股票的最佳时机 III

Difficulty: 困难

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

Solution

动态优化 解决 k次买卖问题

空间 O(n*k)

class Solution {
    public int maxProfit(int[] prices) {
        if(prices==null||prices.length==0) return 0;
    	int K=2; //本题为k次
    	//K+1是为了后面k为0时准备的(int数组初始化默认为0)
    	int[][][]dp=new int[prices.length][K+1][2];
    	// 初始化第一天
    	for(int k=0;k!=K+1;k++) {
    		dp[0][k][0]=0;
    		dp[0][k][1]=-prices[0];
    	}
    	for(int i=1;i!=prices.length;i++) {
    		//k 表示 此时为第k次买|卖
    		for(int k=K;k!=0;k--) {
    			//前一天也是手里没股票 or前一天手里有股票但今天卖出去了
    			dp[i][k][0]=Math.max(dp[i-1][k][0],dp[i-1][k][1]+prices[i]);
        		//前一天时就手里有股票or前一天手里没股票,今天才买的(本金为上次累积的)
    			dp[i][k][1]=Math.max(dp[i-1][k][1],dp[i-1][k-1][0]-prices[i]);	
    		}
    	}
    	return dp[prices.length-1][K][0];
    }
}  

188. 最多k笔:买卖股票的最佳时机 IV

Difficulty: 困难

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

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

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

示例 1:

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

示例 2:

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

Solution

Language: Java

class Solution {
    public int maxProfit(int k,int[] prices) {
        if(prices==null||prices.length==0) return 0;
        //如果k大于prices的一半了,不可能满足 "必须在再次购买前出售掉之前的股票" 这个要求,相当于不限次数买卖
        if(k>prices.length/2) return maxProfitInf(prices);
    	//K+1是为了后面k为0时准备的(int数组初始化默认为0)
    	int[][][]dp=new int[prices.length][k+1][2];
    	// 初始化第一天
    	for(int time=0;time!=k+1;time++) {
    		dp[0][time][0]=0;
    		dp[0][time][1]=-prices[0];
    	}
    	for(int i=1;i!=prices.length;i++) {
    		//k 表示 此时为第k次买|卖
    		for(int time=k;time!=0;time--) {
    			//前一天也是手里没股票 or前一天手里有股票但今天卖出去了
    			dp[i][time][0]=Math.max(dp[i-1][time][0],dp[i-1][time][1]+prices[i]);
        		//前一天时就手里有股票or前一天手里没股票,今天才买的(本金为上次累积的)
    			dp[i][time][1]=Math.max(dp[i-1][time][1],dp[i-1][time-1][0]-prices[i]);	
    		}
    	}
    	return dp[prices.length-1][k][0];
    }

	private int maxProfitInf(int[] prices) {
    	int yesterdayNo=0,yesterdayYes=-prices[0];
    	for(int i=1;i!=prices.length;i++) {
    		//前一天也是手里没股票 or前一天手里有股票但今天卖出去了
    		yesterdayNo=Math.max(yesterdayNo,yesterdayYes+prices[i]);
    		//前一天时就手里有股票or前一天手里没股票,今天才买的(本金为上次累积的)
    		yesterdayYes=Math.max(yesterdayYes,yesterdayNo-prices[i]);
    	}
    	return yesterdayNo;
	}
} 

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

Difficulty: 中等

给定一个整数数组,其中第_ i_ 个元素代表了第 i 天的股票价格 。​

设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

  • 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
  • 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

示例:

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

Solution

O(n)空间复杂度的

class Solution {
    //多次买卖一支股票 相当于买卖不限次数(最大price.length/2)
    //冷冻期为 1 天 所以状态转移时有i-2
    public int maxProfit(int[] prices) {
        //长度不够 不能买卖
        if(prices==null||prices.length<=1) return 0;
        int[][]dp=new int[prices.length][2];
        dp[0][0]=0;dp[0][1]=-prices[0];
        //前一天没持有股票OR 前一天持有股票了但今天卖了
        dp[1][0]=Math.max(0,-prices[0]+prices[1]);
        //前一天持有股票但今天还没卖 OR 前一天没持有股票,今天持有的
        dp[1][1]=Math.max(-prices[0],0-prices[1]);// 其实就是在第一天和第二天取小的那个买(这里取负数了所以有点难理解)
        for(int i=2;i!=prices.length;i++) {
        	dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
        	// 昨天已经持有了OR前天已经前天前没有持有(避开冷冻期),今天再买
        	dp[i][1]=Math.max(dp[i-1][1],dp[i-2][0]-prices[i]);
        }
        return dp[prices.length-1][0];
    }
} 

O(1)空间复杂度

class Solution {
    //多次买卖一支股票 相当于买卖不限次数(最大price.length/2)
    //冷冻期为 1 天 所以状态转移时有i-2
    public int maxProfit(int[] prices) {
        //长度不够 不能买卖
        if(prices==null||prices.length<=1) return 0;
        //还没买呢, 怎么可能拥有,为负无穷,利于max操作
        int yesterdayYes=Integer.MIN_VALUE;
        int yesterdayNo=0;
        //前天卖了或者前天之前买了,初始化为0(因为没有持有股票)
        int yesterday2No=0;
        for(int i=0;i!=prices.length;i++) {
        	int tmp=yesterdayNo;
        	//昨天没有持有OR前天没有持有昨天也没持有,今天买了
        	yesterdayYes=Math.max(yesterdayYes,yesterday2No-prices[i]);
        	yesterdayNo=Math.max(yesterdayNo,yesterdayYes+prices[i]);
        	yesterday2No=tmp;//昨天变前天了
        }
        return yesterdayNo;
    }
} 

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

Difficulty: 中等

给定一个整数数组 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.

注意:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.

Solution

Language: Java

class Solution {
    //无限次地完成交易
    public int maxProfit(int[] prices, int fee) {
    	if(prices==null||prices.length<=1) return 0;
    	int[][]dp=new int[prices.length][2];
    	dp[0][0]=0;dp[0][1]=-prices[0]-fee;
    	for(int i=1;i!=prices.length;i++) {
    		dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
    		dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]-prices[i]-fee);
    	}
    	return dp[prices.length-1][0];
    }
}

参考link(这是真大佬)

  • https://github.com/labuladong/fucking-algorithm/blob/master/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%B3%BB%E5%88%97/%E5%9B%A2%E7%81%AD%E8%82%A1%E7%A5%A8%E9%97%AE%E9%A2%98.md
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章