Skip to content

Commit 1612c27

Browse files
author
binbin.hou
committed
add for stock
1 parent 1b47a96 commit 1612c27

12 files changed

+414
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com..houbb.leetcode.F100T200;
2+
3+
public class T121_BestTimeToBuyAndSellStock_V20241105 {
4+
5+
/**
6+
* 思虑
7+
*
8+
* 1. 从第一天作为买入,和后面的每一天进行对比
9+
* 2. 获取出最大的差值
10+
* 问题:问题出现在重复计算。
11+
*
12+
*
13+
* 其实一种比较自然的算法是双指针。
14+
*
15+
* L R,时间复杂度为 O(N)
16+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735550/python-javascript-easy-solution-with-very-clear-explanation/
17+
*
18+
* @param prices 价格数组
19+
* @return 结果
20+
*/
21+
public int maxProfit(int[] prices) {
22+
int b1 = -prices[0];
23+
int s1 = 0;
24+
25+
for(int i = 0; i < prices.length; i++) {
26+
// 卖出第一笔 是否卖?
27+
s1 = Math.max(s1, b1 + prices[i]);
28+
// 买入第一笔 是否买?
29+
b1 = Math.max(b1, - prices[i]);
30+
}
31+
32+
return s1;
33+
}
34+
35+
public static void main(String[] args) {
36+
int[] prices = new int[]{7,1,5,3,6,4};
37+
int value = new T121_BestTimeToBuyAndSellStock_V20241105().maxProfit(prices);
38+
System.out.println(value);
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com..houbb.leetcode.F100T200;
2+
3+
public class T122_BestTimeToBuyAndSellStockII_DP {
4+
5+
/**
6+
* DP 思路
7+
*/
8+
9+
public int maxProfit(int[] prices) {
10+
int buy[] = new int[prices.length];
11+
int sell[] = new int[prices.length];
12+
13+
buy[0] = -prices[0];
14+
15+
for(int i = 1; i < prices.length; i++) {
16+
// 是否卖出? 不卖; 卖出=上一次买入 + 当前价格
17+
sell[i] = Math.max(sell[i-1], buy[i-1] + prices[i]);
18+
19+
// 是否买? 不买; 买入=上一次卖出-当前价格
20+
buy[i] = Math.max(buy[i-1], sell[i-1] - prices[i]);
21+
}
22+
23+
return sell[prices.length-1];
24+
}
25+
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com..houbb.leetcode.F100T200;
2+
3+
public class T123_BestTimeToBuyAndSellStockIII_V20241105 {
4+
5+
6+
/**
7+
* 递归算法,不考虑时间
8+
*
9+
* 1. 最多只能做 4 次交易。2次买入,2次卖出
10+
* 2. 一天最多2种决策:
11+
*
12+
* 2.1 不做交易
13+
* 2.2 做交易:买入/卖出
14+
*
15+
* 3. 时间在流逝
16+
*
17+
*
18+
* @param prices 价格数组
19+
* @return 结果
20+
*/
21+
public int maxProfit(int[] prices) {
22+
int maxProfit = 0;
23+
// 最小值
24+
int min = prices[0];
25+
int in1_out1_max = 0;
26+
// 买1卖1买2
27+
int in1_out1_in2_max = 0;
28+
for(int i = 0; i < prices.length; i++) {
29+
// 计算出当前的结果
30+
maxProfit = Math.max(maxProfit, in1_out1_in2_max+prices[i]);
31+
32+
min = Math.min(prices[i], min);
33+
// 第一题的思路,记录最大值
34+
in1_out1_max = Math.max(prices[i] - min, in1_out1_max);
35+
36+
// 下一次的操作
37+
// 去掉当前这一次的金额,准备好对应的金额
38+
in1_out1_in2_max = Math.max(in1_out1_in2_max, in1_out1_max - prices[i]);
39+
}
40+
41+
return maxProfit;
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com..houbb.leetcode.F100T200;
2+
3+
public class T123_BestTimeToBuyAndSellStockIII_V20241105_VDP {
4+
5+
6+
/**
7+
* 递归算法,不考虑时间
8+
*
9+
* 1. 最多只能做 4 次交易。2次买入,2次卖出
10+
* 2. 一天最多2种决策:
11+
*
12+
* 2.1 不做交易
13+
* 2.2 做交易:买入/卖出
14+
*
15+
* 3. 时间在流逝
16+
*
17+
*
18+
* @param prices 价格数组
19+
* @return 结果
20+
*/
21+
public int maxProfit(int[] prices) {
22+
int b1 = -prices[0];
23+
int b2 = -prices[0];
24+
int s1 = 0;
25+
int s2 = 0;
26+
27+
for(int i = 0; i < prices.length; i++) {
28+
// 卖出第二笔 是否卖?
29+
s2 = Math.max(s2, b2 + prices[i]);
30+
// 买入第二笔 是否买?
31+
b2 = Math.max(b2, s1 - prices[i]);
32+
// 卖出第一笔 是否卖?
33+
s1 = Math.max(s1, b1 + prices[i]);
34+
// 买入第一笔 是否买?
35+
b1 = Math.max(b1, - prices[i]);
36+
}
37+
38+
return s2;
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com..houbb.leetcode.F100T200;
2+
3+
public class T188_BestTimeToBuyAndSellStockIV_v20241105_VDP {
4+
5+
/**
6+
* 优化思路:通过 DP 替代掉递归。
7+
*
8+
* @param prices 价格数组
9+
* @return 结果
10+
*/
11+
public int maxProfit(int k, int[] prices) {
12+
// k+1 是为了让 k 和 下标相同,-1 也可以。
13+
int[] buy = new int[k + 1];
14+
int[] sell = new int[k + 1];
15+
// 初始化买入状态为最小值,表示极小的初始损失
16+
for (int i = 0; i <= k; i++) {
17+
buy[i] = -prices[0];
18+
}
19+
20+
// for-each 数组本身
21+
for (int i = 1; i < prices.length; i++) {
22+
23+
// for-each k 次操作
24+
for (int j = 1; j <= k; j++) {
25+
// 卖出第 j 次 不卖 OR 卖出:当前买入+介个
26+
sell[j] = Math.max(sell[j], buy[j] + prices[i]);
27+
// 买入第 j 次 不买 OR 买入=上一次-当前
28+
buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]);
29+
}
30+
31+
}
32+
33+
return sell[k];
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com..houbb.leetcode.F200T300;
2+
3+
public class T212_bestTimeToBuyAndSellStock {
4+
5+
/**
6+
* 最简单的暴力算法
7+
* @param prices 价格
8+
* @return 结果
9+
*/
10+
public int maxProfit(int[] prices) {
11+
int maxResult = 0;
12+
13+
for(int i = 0; i < prices.length-1; i++) {
14+
for(int j = i+1; j < prices.length; j++) {
15+
int profit = prices[j] - prices[i];
16+
maxResult = Math.max(profit, maxResult);
17+
}
18+
}
19+
20+
return maxResult;
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com..houbb.leetcode.F200T300;
2+
3+
public class T212_bestTimeToBuyAndSellStockBest {
4+
5+
public int maxProfit(int[] prices) {
6+
int maxResult = 0;
7+
int minVal = prices[0];
8+
for(int i = 0; i < prices.length; i++) {
9+
minVal = Math.min(minVal, prices[i]);
10+
maxResult = Math.max(prices[i] - minVal, maxResult);
11+
}
12+
13+
return maxResult;
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com..houbb.leetcode.F200T300;
2+
3+
public class T212_bestTimeToBuyAndSellStock_II {
4+
5+
/**
6+
* 最简单的暴力算法
7+
* @param prices 价格
8+
* @return 结果
9+
*/
10+
public int maxProfit(int[] prices) {
11+
int maxResult = 0;
12+
int minVal = prices[0];
13+
int maxVal = prices[0];
14+
for(int i = 1; i < prices.length; i++) {
15+
int cur = prices[i];
16+
// 值大于当前值
17+
if(cur > maxVal) {
18+
maxResult = Math.max(maxResult, cur - minVal);
19+
}
20+
// 重置
21+
if(cur < minVal) {
22+
minVal = cur;
23+
maxVal = cur;
24+
}
25+
}
26+
27+
return maxResult;
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com..houbb.leetcode.F200T300;
2+
3+
public class T213_bestTimeToBuyAndSellStock_III {
4+
5+
/**
6+
* 最简单的暴力算法
7+
* @param prices 价格
8+
* @return 结果
9+
*/
10+
public int maxProfit(int[] prices) {
11+
int maxResult = 0;
12+
int minVal = prices[0];
13+
int maxVal = prices[0];
14+
for(int i = 1; i < prices.length; i++) {
15+
int cur = prices[i];
16+
// 值大于当前值
17+
if(cur > maxVal) {
18+
maxResult = Math.max(maxResult, cur - minVal);
19+
}
20+
// 重置
21+
if(cur < minVal) {
22+
minVal = cur;
23+
maxVal = cur;
24+
}
25+
}
26+
27+
return maxResult;
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com..houbb.leetcode.F300T400;
2+
3+
/**
4+
* @author binbin.hou
5+
* @since 1.0.0
6+
*/
7+
public class T309_bestTimeToBuyAndSellStockWithCooldownV1 {
8+
9+
10+
public int maxProfit(int[] prices) {
11+
int n = prices.length;
12+
int[] dp = new int[n];
13+
14+
for (int i = 1; i < n; i ++) {
15+
if (prices[i] <= prices[i-1]) {
16+
dp[i] = dp[i-1];
17+
continue;
18+
}
19+
if (i < 2) {
20+
dp[i] = prices[1] - prices[0];
21+
} else {
22+
dp[i] = Math.max(dp[i-1], dp[i-2] + prices[i] - prices[i-1]);
23+
}
24+
}
25+
26+
return dp[n-1];
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)