class Solution {
public int maxProfit(int[] prices, int fee) {
int n = prices.length;
return tabOpt(prices, n, fee);
}
public int tabOpt(int[] pr, int n, int f) {
int[] dp = new int[2];
for (int i = n - 1; i >= 0; i--) {
int[] cur = new int[2];
for (int b = 1; b >= 0; b--) {
if (b == 1) {
cur[b] = Math.max(-pr[i] + dp[1 - b], dp[b]);
} else {
cur[b] = Math.max(pr[i] - f + dp[1 - b], dp[b]);
}
}
dp = cur;
}
return dp[1];
}
public int tab(int[] pr, int n, int f) {
int[][] dp = new int[n + 1][2];
for (int i = n - 1; i >= 0; i--) {
for (int b = 1; b >= 0; b--) {
if (b == 1) {
dp[i][b] = Math.max(-pr[i] + dp[i + 1][1 - b], dp[i + 1][b]);
} else {
dp[i][b] = Math.max(pr[i] - f + dp[i + 1][1 - b], dp[i + 1][b]);
}
}
}
return dp[0][1];
}
public int recur(int i, int b, int[] pr, int n, int f, int[][] dp) {
if (i == n)
return 0;
if (dp[i][b] != -1)
return dp[i][b];
if (b == 1) {
return dp[i][b] = Math.max(-pr[i] + recur(i + 1, 1 - b, pr, n, f, dp), recur(i + 1, b, pr, n, f, dp));
} else {
return dp[i][b] = Math.max(pr[i] - f + recur(i + 1, 1 - b, pr, n, f, dp), recur(i + 1, b, pr, n, f, dp));
}
}
}