Leetcode
上有一个买卖股票系列的算法问题,主要区别在因而否有交易次数限制、是否交易有冷却期、是否有交易手续费等条件。本文探究的就是这个系列的通用思路和解法、不一样条件时的修改以及最优解。阅读本文须要事先对这个系列各个问题的题目有必定的了解,了解动态规划
。本文会从最复杂的条件开始,得出最通用的解法,因此一开始反而是最难的,推荐有兴趣、有耐心的读者先从头至尾阅读一遍,若是难以理解的话,再从最简单的条件开始阅读,这样就能够深入了解这个系列的解题思路,掌握解题模板。本文代码使用的语言是Java
。java
定义一个二维数组int[][] profit = new int[n][2]
,其中profit[i][0]
表示第i
天卖出股票(没有持有股票)时的最大收益,profit[i][1]
表示表示第i
天买入股票(持有股票)时的最大收益算法
那么状态转移方程是:数组
// 第i天的卖出状态 = Max(前一天卖出状态,前一天买入状态 + 卖出股票得到的收益)
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]);
// 第i天的买入状态 = Max(前一天买入状态,前一天前一次已卖出状态 - 买入股票扣除的收益)
profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - prices[i]);
复制代码
这个系列全部问题的解答都是基于这个状态转移方程函数
如下代码是包含了Leetcode
上买卖股票系列全部不一样条件下的通用解优化
// k表示交易次数,fee表示交易手续费,m表示交易冷却期
public int maxProfit(int k, int[] prices, int fee, int m) {
if (k == 0 || prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
// 进行一次彻底的交易须要两天,因此当 k > n/2 的时候,就能够天天都进行一次买入(卖出)操做,也就是能够交易无数次
if (k > (n >> 1)) {
int[][] profit = new int[n][2];
for (int i = 0; i < n; i++) {
// 处理初始状态
if (i == 0) {
profit[i][0] = 0;
profit[i][1] = -prices[0];
continue;
}
// 处理有交易冷却期时,前 m + 1 天的状况
if (i < m + 1) {
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i] - fee);
profit[i][1] = Math.max(profit[i - 1][1], 0 - prices[i]);
continue;
}
// 核心,状态转移方程
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i] - fee);
profit[i][1] = Math.max(profit[i - 1][1], profit[i - (m + 1)][0] - prices[i]);
}
return profit[n - 1][0];
}
int[][][] profit = new int[n][k + 1][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < k + 1; j++) {
// 处理初始状态
if (i == 0) {
profit[i][j][0] = 0;
profit[i][j][1] = -prices[0];
continue;
}
if (j == 0) {
profit[i][j][0] = 0;
profit[i][j][1] = -prices[0];
continue;
}
// 处理有交易冷却期时,前 m + 1 天的状况
if (i < m + 1) {
profit[i][j][0] = Math.max(profit[i - 1][j][0], profit[i - 1][j][1] + prices[i] - fee);
profit[i][j][1] = Math.max(profit[i - 1][j][1], 0 - prices[i]);
continue;
}
// 核心,状态转移方程
profit[i][j][0] = Math.max(profit[i - 1][j][0], profit[i - 1][j][1] + prices[i] - fee);
profit[i][j][1] = Math.max(profit[i - 1][j][1], profit[i - (m + 1)][j - 1][0] - prices[i]);
}
}
return profit[n - 1][k][0];
}
复制代码
从上面函数能够看出,i
表示的天数这一维度能够省略,但若是有交易冷却期这个条件的话,须要额外添加一个数组来保存[i - (m + 1), i - 1]
天前的值,优化后的代码以下spa
public int maxProfit(int k, int[] prices, int fee, int m) {
if (k == 0 || prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
if (k > (n >> 1)) {
int sell = 0;
int buy = Integer.MIN_VALUE + fee;
// 保存 [i - (m + 1), i - 1] 天前的值
int[] preSells = new int[m + 1];
for (int i = 0; i < prices.length; i++) {
sell = Math.max(sell, buy + prices[i] - fee);
buy = Math.max(buy, preSells[i % (m + 1)] - prices[i]);
preSells[i % (m + 1)] = sell;
}
return sell;
}
int[] sells = new int[k];
int[] buys = new int[k];
// 保存 [i - (m + 1), i - 1] 天前的值
int[][] preSells = new int[k][m + 1];
// 处理初始状态
for (int i = 0; i < k; i++) {
sells[i] = 0;
buys[i]= Integer.MIN_VALUE + fee;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (j == 0) {
sells[j] = Math.max(sells[j], buys[j] + prices[i] - fee);
buys[j] = Math.max(buys[j], -prices[i]);
preSells[j][i % (m + 1)] = sells[j];
continue;
}
sells[j] = Math.max(sells[j], buys[j] + prices[i] - fee);
buys[j] = Math.max(buys[j], preSells[j - 1][i % (m + 1)] - prices[i]);
preSells[j][i % (m + 1)] = sells[j];
}
}
return sells[k - 1];
}
复制代码
这个系列全部问题均可以在上面的代码基础上进行修改优化,去除没必要要的代码便可得出解code
Leetcode
的188题it
因为只有一个交易次数的条件,因此不须要m
,也不须要fee
,直接简化代码便可io
public int maxProfit(int k, int[] prices) {
if (k == 0 || prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
// 进行一次彻底的交易须要两天,因此当 k > n/2 的时候,就能够天天都进行一次买入(卖出)操做,也就是能够交易无数次
if (k > (n >> 1)) {
int[][] profit = new int[n][2];
for (int i = 0; i < n; i++) {
if (i == 0) {
profit[i][0] = 0;
profit[i][1] = -prices[0];
continue;
}
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]);
profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - prices[i]);
}
return profit[n - 1][0];
}
int[][][] profit = new int[n][k + 1][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < k + 1; j++) {
if (i == 0) {
profit[i][j][0] = 0;
profit[i][j][1] = -prices[0];
continue;
}
if (j == 0) {
profit[i][j][0] = 0;
profit[i][j][1] = -prices[0];
continue;
}
profit[i][j][0] = Math.max(profit[i - 1][j][0], profit[i - 1][j][1] + prices[i]);
profit[i][j][1] = Math.max(profit[i - 1][j][1], profit[i - 1][j - 1][0] - prices[i]);
}
}
return profit[n - 1][k][0];
}
复制代码
优化function
public int maxProfit(int k, int[] prices) {
if (k == 0 || prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
if (k > (n >> 1)) {
int sell = 0;
int buy = Integer.MIN_VALUE;
for (int price : prices) {
sell = Math.max(sell, buy + price);
buy = Math.max(buy, sell - price);
}
return sell;
}
int[] sells = new int[k];
int[] buys = new int[k];
for (int i = 0; i < k; i++) {
sells[i] = 0;
buys[i]= Integer.MIN_VALUE;
}
for (int price : prices) {
for (int i = 0; i < k; i++) {
if (i == 0) {
sells[i] = Math.max(sells[i], buys[i] + price);
buys[i] = Math.max(buys[i], -price);
continue;
}
sells[i] = Math.max(sells[i], buys[i] + price);
buys[i] = Math.max(buys[i], sells[i - 1] - price);
}
}
return sells[k - 1];
}
复制代码
Leetcode
的123题
因为只有一个交易次数的条件,因此不须要m
,也不须要fee
,直接简化代码得
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int k = 2;
int n = prices.length;
int[][][] profit = new int[n][k + 1][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < k + 1; j++) {
if (i == 0) {
profit[i][j][0] = 0;
profit[i][j][1] = -prices[0];
continue;
}
if (j == 0) {
profit[i][j][0] = 0;
profit[i][j][1] = -prices[0];
continue;
}
profit[i][j][0] = Math.max(profit[i - 1][j][0], profit[i - 1][j][1] + prices[i]);
profit[i][j][1] = Math.max(profit[i - 1][j][1], profit[i - 1][j - 1][0] - prices[i]);
}
}
return profit[n - 1][k][0];
}
复制代码
优化,因为只能交易两次,因此只须要两组变量来保存结果便可
public int maxProfit(int[] prices) {
int sell1 = 0;
int buy1 = Integer.MIN_VALUE;
int sell2 = 0;
int buy2 = Integer.MIN_VALUE;
for (int price : prices) {
sell1 = Math.max(sell1, buy1 + price);
buy1 = Math.max(buy1, -price);
sell2 = Math.max(sell2, buy2 + price);
buy2 = Math.max(buy2, sell1 - price);
}
return sell2;
}
复制代码
Leetcode
的121题
因为只有一个交易次数的条件,因此不须要m
,也不须要fee
;同时由于只能交易一次,因此k = 1
,也就是能够省略
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
int[][] profit = new int[n][2];
for (int i = 0; i < n; i++) {
if (i == 0) {
profit[i][0] = 0;
profit[i][1] = -prices[0];
continue;
}
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]);
// 由于只能交易一次,因此这里的原来的 profit[i - 1][0] 恒等于 0
profit[i][1] = Math.max(profit[i - 1][1], -prices[i]);
}
return profit[n - 1][0];
}
复制代码
优化
public int maxProfit(int[] prices) {
int sell = 0;
int buy = Integer.MIN_VALUE;
for (int price : prices) {
sell = Math.max(sell, buy + price);
buy = Math.max(buy, -price);
}
return sell;
}
复制代码
这个题目有更通俗直接的解法
// 遍历的同时,保存数组中的最小值,更新最大差值
public int maxProfit(int[] prices) {
int profit = 0;
int min = Integer.MAX_VALUE;
for (int price : prices) {
if (price < min) {
min = price;
} else if (price - min > profit) {
profit = price - min;
}
}
return profit;
}
复制代码
Leetcode
的122题
因为只一个交易次数的条件,因此不须要m
,也不须要fee
;至于k
这一维度,也能够删除,有两种理解方式
k = +∞
,这时候k ≈ k - 1
,因此k
能够认为是没有做用的,能够删除k
这一维度k
这一维度是由于有交易次数限制,天天都要进行遍历,从k
次交易内选择最优方案;但若是没有交易次数限制,则能够认为天天都进行交易,收益能够一直累加,下一天直接取以前的最优方案便可,因此能够删除k
这一维度public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
int[][] profit = new int[n][2];
for (int i = 0; i < n; i++) {
if (i == 0) {
profit[i][0] = 0;
profit[i][1] = -prices[0];
continue;
}
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]);
// 这里的 profit[i - 1][0] 表示前一天的最优方案,由于没有交易次数限制,也就是收益能够一直累加
profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - prices[i]);
}
return profit[n - 1][0];
}
复制代码
优化
public int maxProfit(int[] prices) {
int sell = 0;
int buy = Integer.MIN_VALUE;
for (int price : prices) {
sell = Math.max(sell, buy + price);
buy = Math.max(buy, sell - price);
}
return sell;
}
复制代码
能够看出只能交易一次与能够交易无数次的区别:
更通俗直接的解法,贪心
// 因为不限交易次数,因此只要后面的价格比较大,都能得到收益
public int maxProfit(int[] prices) {
int maxProfit = 0;
for (int i = 0; i < prices.length - 1; i++) {
int today = prices[i];
int tomorrow = prices[i + 1];
if (today < tomorrow) {
maxProfit += tomorrow - today;
}
}
return maxProfit;
}
复制代码
Leetcode
的309题
在能够交易无数次的条件上,加上m
这个条件便可
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
// 若是冷却期是n天的话,让 m = n 便可
int m = 1;
int n = prices.length;
int[][] profit = new int[n][2];
for (int i = 0; i < n; i++) {
if (i == 0) {
profit[i][0] = 0;
profit[i][1] = -prices[0];
continue;
}
if (i < m + 1) {
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]);
profit[i][1] = Math.max(profit[i - 1][1], 0 - prices[i]);
continue;
}
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i]);
profit[i][1] = Math.max(profit[i - 1][1], profit[i - (m + 1)][0] - prices[i]);
}
return profit[n - 1][0];
}
复制代码
优化
public int maxProfit(int[] prices) {
// 若是冷却期是n天的话,让 m = n 便可
int m = 1;
int sell = 0;
int buy = Integer.MIN_VALUE;
int[] preSells = new int[m + 1];
for (int i = 0; i < prices.length; i++) {
sell = Math.max(sell, buy + prices[i]);
buy = Math.max(buy, preSells[i % (m + 1)] - prices[i]);
preSells[i % (m + 1)] = sell;
}
return sell;
}
复制代码
Leetcode
的714题
在能够交易无数次的条件上,加上fee
这个条件便可
public int maxProfit(int[] prices, int fee) {
if (prices == null || prices.length < 2) {
return 0;
}
int n = prices.length;
int[][] profit = new int [n][2];
for (int i = 0; i < n; i++) {
if (i == 0) {
profit[i][0] = 0;
profit[i][1] = -prices[0];
continue;
}
profit[i][0] = Math.max(profit[i - 1][0], profit[i - 1][1] + prices[i] - fee);
profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - prices[i]);
}
return profit[n - 1][0];
}
复制代码
优化
public int maxProfit(int[] prices) {
int sell = 0;
// 以防溢出
int buy = Integer.MIN_VALUE + fee;
for (int price : prices) {
sell = Math.max(sell, buy + price - fee);
buy = Math.max(buy, sell - price);
}
return sell;
}
复制代码
从最复杂的条件开始思考这个系列解法或许有点反人类,可是我我的以为这样才是最能全局深入理解掌握这个系列的办法,最复杂的状况都解决了,剩下的就很简单了。相信各位读者看完以上这么多不一样条件下的解法以及优化后的代码,必定会对动态规划
有更深的理解,若是有更优的解法,欢迎一块儿探讨。