-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
47 lines (46 loc) · 1.17 KB
/
Solution.cs
File metadata and controls
47 lines (46 loc) · 1.17 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Solution
{
int[] prices;
long[,,] memo;
public long MaximumProfit(int[] prices, int k)
{
this.prices = prices;
int n = prices.Length;
memo = new long[n, k + 1, 3];
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= k; j++)
{
for (int s = 0; s < 3; s++)
{
memo[i, j, s] = -1;
}
}
}
return DP(n - 1, k, 0);
}
long DP(int i, int j, int state)
{
if (j == 0) return 0;
if (i == 0)
{
return state == 0 ? 0 : (state == 1 ? -prices[0] : prices[0]);
}
long res = memo[i, j, state];
if (res != -1) return res;
int p = prices[i];
if (state == 0)
{
res = Math.Max(DP(i - 1, j, 0), Math.Max(DP(i - 1, j, 1) + p, DP(i - 1, j, 2) - p));
}
else if (state == 1)
{
res = Math.Max(DP(i - 1, j, 1), DP(i - 1, j - 1, 0) - p);
}
else
{
res = Math.Max(DP(i - 1, j, 2), DP(i - 1, j - 1, 0) + p);
}
return memo[i, j, state] = res;
}
}