Say you have an array for which the i-th element is the price of a given stock on day i
.
Design an algorithm to find the maximum profit. You may complete at most k
transactions.
DP Solution
Let dp[i][j]
to be the maximum profit to buy and sell the stock with at most i
operations at t=j
.
For any given time t
, we have two options:
- Do nothing, or buy the stock, the profit remains unchanged:
dp[i][j] = dp[i][j-1]
- Sell the stock. We must buy the stock first. Assume that we but the stock at $t \in [0, j-1]$
- We have already gained profit
dp[i-1][t-1]
- Buy the stock:
-prices[t]
- Sell the stock:
prices[j]
Total profit:dp[i-1][t-1] - prices[t] + prices[j]
- We have already gained profit
- We need to iterate through all possible buying time:
$$ dp[i][j] = \max \left\lbrace dp[i][j-1], \max\limits_{t \in [0, j-1]} \left\lbrace dp[i-1][t-1] - p[t] \right\rbrace + p[j] \right\rbrace $$
Notice the 2nd term, we let $M_j=\max\limits_{t \in [0, j-1]} \left\lbrace dp[i-1][t-1] - p[t] \right\rbrace$ and $ m_{t} = dp[i-1][t-1] - p[t] $:
$$ M_{j} = \max\limits_{t \in [0, j-1]} \lbrace m_{t} \rbrace = \max \left\lbrace M_{j-1}, m_{j-1} \right\rbrace $$
At each iteration $i, j$, we could calculate $m_{j}=dp[i-1][j-1]-p[j]$ for the next iteration in advance. Because in current iteration we already have the data of dp[i-1][x]
. Thus we can get rid of the inner loop and calculate $M_j$ in an iterative way.
Corner Cases
We start from $i=1, j=1$ to avoid corner cases. For $i=0$ or $j=0$, $dp[i][j]=0$, so we don’t need additional init steps.
|
|