2
public int maxProfit(int[] prices, int fee) {3
int[][] dp = new int[prices.length][2];8
return profit(prices, fee, 0, 1, dp);11
public int profit(int[] prices, int fee, int i, int buy, int[][] dp) {12
if (i == prices.length) {15
if (dp[i][buy] != -1) {20
int yesBuy = profit(prices, fee, i + 1, 0, dp) - prices[i];21
int noBuy = profit(prices, fee, i + 1, 1, dp);22
maxProfit = Math.max(yesBuy, noBuy);24
int yesSell = prices[i] - fee + profit(prices, fee, i + 1, 1, dp);25
int noSell = profit(prices, fee, i + 1, 0, dp);26
maxProfit = Math.max(yesSell, noSell);28
return dp[i][buy] = maxProfit;