Skip to content

Commit 72418c3

Browse files
committed
动态规划算法
1 parent 0afc141 commit 72418c3

File tree

4 files changed

+146
-3
lines changed

4 files changed

+146
-3
lines changed

codes/algorithm/src/main/java/io/github/dunwu/algorithm/dynamic/买卖股票的最佳时机II.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ public static void main(String[] args) {
1818

1919
public static int maxProfit(int[] prices) {
2020
if (prices == null || prices.length == 0) return 0;
21+
2122
int max = 0;
22-
int n = prices.length;
23-
int[][] dp = new int[n][2];
23+
final int days = prices.length;
24+
final int[][] dp = new int[days][2];
25+
2426
dp[0][0] = 0;
2527
dp[0][1] = -prices[0];
26-
for (int i = 1; i < n; i++) {
28+
29+
for (int i = 1; i < days; i++) {
2730
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
2831
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
2932
max = Math.max(dp[i][0], dp[i][1]);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.github.dunwu.algorithm.dynamic;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
5+
/**
6+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
7+
* @see <a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/">188. 买卖股票的最佳时机 IV</a>
8+
* @since 2020-07-05
9+
*/
10+
public class 买卖股票的最佳时机IV {
11+
12+
public static void main(String[] args) {
13+
int[] prices = { 2, 4, 1 };
14+
int[] prices2 = { 3, 2, 6, 5, 0, 3 };
15+
Assertions.assertEquals(2, maxProfit(2, prices));
16+
Assertions.assertEquals(7, maxProfit(2, prices2));
17+
}
18+
19+
public static int maxProfit(final int k, int[] prices) {
20+
if (prices == null || prices.length == 0) return 0;
21+
final int days = prices.length;
22+
if (k > days / 2) return maxProfit(prices);
23+
24+
// 定义二维数组
25+
// 一维表示第 i 天
26+
// 二维表示交易笔数,最多 2 笔
27+
// 三维表示是否持有股票:0/1(持有)
28+
int[][][] dp = new int[days][k + 1][2];
29+
30+
// 第一天数据初始化
31+
for (int j = 0; j <= k; j++) {
32+
dp[0][j][0] = 0;
33+
dp[0][j][1] = -prices[0];
34+
}
35+
36+
for (int i = 1; i < days; i++) { // 扫描天数
37+
for (int j = k; j >= 1; j--) {
38+
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
39+
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
40+
}
41+
}
42+
43+
return dp[days - 1][k][0];
44+
}
45+
46+
public static int maxProfit(int[] prices) {
47+
if (prices == null || prices.length == 0) return 0;
48+
49+
int max = 0;
50+
final int days = prices.length;
51+
final int[][] dp = new int[days][2];
52+
53+
dp[0][0] = 0;
54+
dp[0][1] = -prices[0];
55+
56+
for (int i = 1; i < days; i++) {
57+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
58+
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
59+
max = Math.max(dp[i][0], dp[i][1]);
60+
}
61+
return max;
62+
}
63+
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.dunwu.algorithm.dynamic;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
5+
/**
6+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
7+
* @see <a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/">714.
8+
* 买卖股票的最佳时机含手续费</a>
9+
* @since 2020-07-05
10+
*/
11+
public class 买卖股票的最佳时机含手续费 {
12+
13+
public static void main(String[] args) {
14+
int[] prices = { 1, 3, 2, 8, 4, 9 };
15+
Assertions.assertEquals(8, maxProfit(prices, 2));
16+
}
17+
18+
public static int maxProfit(int[] prices, int fee) {
19+
if (prices == null || prices.length == 0) return 0;
20+
21+
int max = 0;
22+
final int days = prices.length;
23+
final int[][] dp = new int[days][2];
24+
25+
dp[0][0] = 0;
26+
dp[0][1] = -prices[0];
27+
28+
for (int i = 1; i < days; i++) {
29+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
30+
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
31+
max = Math.max(dp[i][0], dp[i][1]);
32+
}
33+
return max;
34+
}
35+
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.github.dunwu.algorithm.dynamic;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
5+
/**
6+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
7+
* @see <a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/">309. 最佳买卖股票时机含冷冻期</a>
8+
* @since 2020-07-05
9+
*/
10+
public class 最佳买卖股票时机含冷冻期 {
11+
12+
public static void main(String[] args) {
13+
int[] prices = { 1, 2, 3, 0, 2 };
14+
Assertions.assertEquals(3, maxProfit(prices));
15+
}
16+
17+
public static int maxProfit(int[] prices) {
18+
if (prices == null || prices.length == 0) return 0;
19+
20+
int max = 0;
21+
final int days = prices.length;
22+
final int[][][] dp = new int[days][2][2];
23+
24+
dp[0][0][0] = 0;
25+
dp[0][0][1] = 0;
26+
dp[0][1][0] = -prices[0];
27+
28+
for (int i = 1; i < days; i++) {
29+
dp[i][0][0] = Math.max(dp[i - 1][0][0], dp[i - 1][0][1]);
30+
dp[i][0][1] = dp[i - 1][1][0] + prices[i];
31+
dp[i][1][0] = Math.max(dp[i - 1][0][0] - prices[i], dp[i - 1][1][0]);
32+
33+
int temp1 = Math.max(dp[i][0][0], dp[i][0][1]);
34+
int temp2 = Math.max(dp[i][1][0], temp1);
35+
max = Math.max(max, temp2);
36+
}
37+
return max;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)