文章目录
买卖股票的最佳时机
买卖股票的最佳时机 II
买卖股票的最佳时机 III
买卖股票的最佳时机 IV
最佳买卖股票时机含冷冻期
买卖股票的最佳时机含手续费
买卖股票的最佳时机
前i天的最大收益 = max{前i-1天的最大收益,第i天的价格-前i-1天中的最小价格}
class Solution {
public int maxProfit(int[] prices) {
int min = prices[0];
int max = prices[0];
int maxprofit1 = 0;
for(int i = 1;i < prices.length;++i){
min = Math.min(min, prices[i]);
maxprofit1 = Math.max(maxprofit1,prices[i] - min);
}
return maxprofit1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
买卖股票的最佳时机 II
题目和第一个题目的差别是,最多同时拥有一个股票,那么这个时候我们的状态就需要分为两种,手中有股票和手中没有股票,到这里我们的dp数组就分为两个集合 有无股票。
class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 1){
return prices[0];
}
int[][] dp = new int[prices.length][2];
dp[0][1] = -prices[0];
//dp[i][1] 表示第 i 天手里有股票的最大收益
//dp[i][0] 表示第 i 天手里没有股票的最大收益
//那么分为前天有股票或者没股票的情况
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][0] - prices[i], dp[i - 1][1]);
}
return dp[prices.length - 1][0];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
买卖股票的最佳时机 III
这个问题在于我们只能完成两笔交易,联想到我们之前的 dp 数组的定义的列为状态,上一道题我们分除了手中有无股票的两种状态,这道题我们也可以继续向这个状态钻研。
可分析出这道题有五个状态,分别是
无操作,第一次买入,第一次卖出,第二次买入,第二次卖出。
我们可以 dp[i][5] 来表示第 i 天,不同状态的最大值。
无操作我们不必多说
第一次买入
有两种情况,分别是①前一天已经买入,②前一天无操作,今天买入,取最大值
dp[i][1] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
第一次卖出
①前一天买入,今天买入 ②前一天第一次卖出
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
第二次买入
同上第一次买入。
dp[i][3] = Math.max(dp[i - 1][2] - prices[i], dp[i - 1][3]);
第二次卖出
同上第一次卖出。
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
很容易发现其实买入和卖出的转移方程是很相似的,这一点很重要,因为我们下个题正是利用了这一点。
public int maxProfit(int[] prices) {
if (prices.length == 1){
return 0;
}
//dp[i][0 - 4] 五种状态
int[][] dp = new int[prices.length][5];
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (int i = 1; i < prices.length; ++i) {
dp[i][1] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
dp[i][3] = Math.max(dp[i - 1][2] - prices[i], dp[i - 1][3]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return dp[prices.length - 1][4];
}//28ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这个dp数组我们只用到了上一次的状态,那我们可以用滚动数组的思考方式来进行一维优化
public int maxProfit(int[] prices) {
if (prices.length == 1){
return 0;
}
//dp[i][0 - 4] 五种状态
int[] dp = new int[5];
dp[1] = dp[3] = -prices[0];
for (int i = 1; i < prices.length; ++i) {
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 dp[4];
}//2ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
买卖股票的最佳时机 IV
这个题和上一道的唯一不同便是2 变成 k,但在上一道题中,我们发现买入和卖出的转移方程是一样的,所以我们按部就班的定义 2 * k + 1 个状态,然后遍历时,判断是买入还是卖出,然后代入上一次的状态即可
public int maxProfit(int k, int[] prices) {
if (prices.length == 1){
return 0;
}
int[] dp = new int[2 * k + 1];
for (int i = 1; i < dp.length; i = i + 2) {
dp[i] = -prices[0];
}
for (int i = 1; i < prices.length; ++i) {
for (int j = 1; j < dp.length; ++j) {
if (j % 2 == 0){
dp[j] = Math.max(dp[j], dp[j - 1] + prices[i]);
}else{
dp[j] = Math.max(dp[j], dp[j - 1] - prices[i]);
}
}
}
return dp[2 * k];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最佳买卖股票时机含冷冻期
四种状态,0手里有,1手里没有(之前卖出),2手里没有(今天卖出),3冷冻期
public int maxProfit(int[] prices) {
int[] dp=new int[4];
dp[0] = -prices[0];
dp[1] = 0;
for(int i = 1; i <= prices.length; i++){
// 使用临时变量来保存dp[0], dp[2]
// 因为马上dp[0]和dp[2]的数据都会变
int temp = dp[0];
int temp1 = dp[2];
//要么买过了,要么今天才买入(①前一天冷冻期,②前一天不是冷冻期但手中没有股票)
dp[0] = Math.max(dp[0], Math.max(dp[3], dp[1]) - prices[i-1]);
//要么昨天是冷冻期,要么昨天不是冷冻期
dp[1] = Math.max(dp[1], dp[3]);
//只能是今天卖出
dp[2] = temp + prices[i-1];
//只能是昨天卖出
dp[3] = temp1;
}
//取三种的最大值
return Math.max(dp[3],Math.max(dp[1],dp[2]));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
买卖股票的最佳时机含手续费
这道题目和《买卖股票的最佳时机 II》 没有什么区别,只是在计算卖出时,减去这个fee即可。
class Solution {
public int maxProfit(int[] prices, int fee) {
if (prices.length < 2){
return 0;
}
//0手里没有,1手里有
int[][] dp = new int[prices.length][2];
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] - fee);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[prices.length - 1][0];
}
}//17ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
也可以进行一维优化
class Solution {
public int maxProfit(int[] prices, int fee) {
if (prices.length < 2){
return 0;
}
//0手里没有,1手里有
int[] dp = new int[2];
dp[1] = -prices[0];
for (int i = 1; i < prices.length; ++i) {
dp[0] = Math.max(dp[0], dp[1] + prices[i] - fee);
dp[1] = Math.max(dp[1], dp[0] - prices[i]);
}
return dp[0];
}
}//6ms