Address: https: //leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv
Step 1: Status Definitions
dp[i][j][K]
: Indicates the first i
day so far, it has been traded j
twice, and the current ownership status of K
the maximum benefit
Description:
- The first
i
day up (from the0
calculation-start, tolen - 1
), the interval is considered[0, i]
, wherelen
an array ofprices
length; - It has been traded
j
twice,j
from0
the calculation starts tok - 1
date; - State ownership
K
(capital) only two values:0
and1
.0
Means no holding,1
represents ownership in order tok
distinguish, the use ofK
(uppercase).
Step 2: state transition equation
The following considerations dp[i][j][0]
and dp[i][j][1]
how you can transfer over.
Dynamic programming is used to solve the problem of multi-stage decision-making, it is the stage where every day, therefore, the state is one of the state before the previous day transfer over.
dp[i][j][0]
: Indicates that this happened day onej
transaction and is not holding.
Special instructions: sign transactions happened in one day, the occurrence of acts of a purchase of shares, ie a transaction occurs.
The classification is based on the discussion: whether shareholders yesterday.
(1) not holding yesterday, today is not holding, indicating that a new transaction has not occurred;
(2) shareholders yesterday, today is not holding, indicating that the deal is over. In both cases, the transaction was.
Both the maximum value, namely:
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i])
Note: the middle that indicates the status of all transactions j
.
dp[i][j][1]
: Indicates that this day has tradedj
times, and holdings.
Classification based on the discussion remains: whether shareholders yesterday.
(1) holdings yesterday, today holding, indicating the new transaction did not occur between the same two days in a trading range;
(2) no shares yesterday, today, shares, indicating that opens up a new deal.
Both the maximum value, namely:
dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i])
Step 3: Initialize
- All holding state values are set to a large negative (the stock should be at least the maximum negative --1), represents the unknown, when not holding the state value is 0;
- Here
i
andj
subscript are-1
, thereforei
, andj
may set up more than one line, in order to avoid complex classification discussion. The following settings only show small line of code. Multi-setting line of code left to the reader to complete.
Step 4: Output
Finally, the last phase of a state, and that state is not ownership, that is dp[len - 1][k - 1][0]
( i
and j
not much time to set up a line).
Step 5: Consider the state of compression
Noting two state transition equation, dependent only on the row of the current row, and j
depend only on j - 1
, but the state value is another table, a first dimension and therefore can be directly cut.
Did not consider the state the first to write a compressed version.
Java code:
public class Solution {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
// 特判
if (k == 0 || len < 2) {
return 0;
}
if (k >= len / 2) {
return greedy(prices, len);
}
// dp[i][j][K]:到下标为 i 的天数为止(从 0 开始),到下标为 j 的交易次数(从 0 开始)
// 状态为 K 的最大利润,K = 0 表示不持股,K = 1 表示持股
int[][][] dp = new int[len][k][2];
// 初始化:把持股的部分都设置为一个较大的负值
for (int i = 0; i < len; i++) {
for (int j = 0; j < k; j++) {
dp[i][j][1] = -9999;
}
}
// 编写正确代码的方法:对两个"基本状态转移方程"当 i - 1 和 j - 1 分别越界的时候,做特殊判断,赋值为 0 即可
for (int i = 0; i < len; i++) {
for (int j = 0; j < k; j++) {
if (i == 0) {
dp[i][j][1] = -prices[0];
dp[i][j][0] = 0;
} else {
if (j == 0) {
dp[i][j][1] = Math.max(dp[i - 1][j][1], -prices[i]);
} else {
// 基本状态转移方程 1
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
}
// 基本状态转移方程 2
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
}
}
}
// 说明:i、j 状态都是前缀性质的,只需返回最后一个状态
return dp[len - 1][k - 1][0];
}
private int greedy(int[] prices, int len) {
// 转换为股票系列的第 2 题,使用贪心算法完成,思路是只要有利润,就交易
int res = 0;
for (int i = 1; i < len; i++) {
if (prices[i - 1] < prices[i]) {
res += prices[i] - prices[i - 1];
}
}
return res;
}
}
Description: status array i
and j
portions respectively may be provided a multi-line, so that the code may have lost some of the determination, implemented at the reader, for comparison.
Then write a compressed version of the state.
Java code:
public class Solution {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
if (k == 0 || len < 2) {
return 0;
}
if (k >= len / 2) {
return greedy(prices, len);
}
int[][] dp = new int[k][2];
for (int j = 0; j < k; j++) {
dp[j][1] = -9999;
}
for (int price : prices) {
for (int j = 0; j < k ; j++) {
if (j == 0) {
dp[j][1] = Math.max(dp[j][1], -price);
} else {
// 基本状态转移方程 1
dp[j][1] = Math.max(dp[j][1], dp[j - 1][0] - price);
}
// 基本状态转移方程 2
dp[j][0] = Math.max(dp[j][0], dp[j][1] + price);
}
}
return dp[k - 1][0];
}
private int greedy(int[] prices, int len) {
int res = 0;
for (int i = 1; i < len; i++) {
if (prices[i - 1] < prices[i]) {
res += prices[i] - prices[i - 1];
}
}
return res;
}
}
Description: The state transition equation can set k + 1
line, the first line is full 0
, interested readers can try.
The following is a semantic version of the state array, there is no essential difference from the above code, the reader for comparison.
Java code:
import java.util.Arrays;
public class Solution12 {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
if (len < 2 || k == 0) {
return 0;
}
if (k >= len / 2) {
return greedy(prices, len);
}
// k 次持股分别的状态
int[] stock = new int[k];
Arrays.fill(stock, -9999);
// k 次不持股(持有现金)分别的状态
int[] cash = new int[k];
for (int price : prices) {
for (int i = 0; i < k; i++) {
stock[i] = Math.max(stock[i], (i > 0 ? cash[i - 1] : 0) - price);
cash[i] = Math.max(cash[i], stock[i] + price);
}
}
return cash[k - 1];
}
private int greedy(int[] prices, int len) {
int maxProfit = 0;
for (int i = 1; i < len; i++) {
if (prices[i] > prices[i - 1]) {
maxProfit += prices[i] - prices[i - 1];
}
}
return maxProfit;
}
}