-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbest-time-to-buy-and-sell-stock-v.java
More file actions
31 lines (28 loc) · 1.07 KB
/
best-time-to-buy-and-sell-stock-v.java
File metadata and controls
31 lines (28 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
class State {
long maxProfit, buyHold, sellHold;
State(long p, long b, long s) {
maxProfit = p;
buyHold = b;
sellHold = s;
}
}
public long maximumProfit(int[] prices, int k) {
int firstPrice = prices[0];
State[] dp = new State[k + 1];
for (int idx = 0; idx <= k; idx++) {
dp[idx] = new State(0, -firstPrice, firstPrice);
}
int n = prices.length;
for (int day = 1; day < n; day++) {
int currPrice = prices[day];
for (int trans = k; trans > 0; trans--) {
long prevProfit = dp[trans - 1].maxProfit;
dp[trans].maxProfit = Math.max(dp[trans].maxProfit, Math.max(dp[trans].buyHold + currPrice, dp[trans].sellHold - currPrice));
dp[trans].buyHold = Math.max(dp[trans].buyHold, prevProfit - currPrice);
dp[trans].sellHold = Math.max(dp[trans].sellHold, prevProfit + currPrice);
}
}
return dp[k].maxProfit;
}
}