问题描述:
代码:
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n==0)
return 0;
int dp0 = -prices[0], dp1 = 0,dp2=0;
for (int i = 1; i < n; ++i) {
int newDp0 = Math.max(dp0, dp2 - prices[i]);
int newDp1 = dp0 + prices[i];
int newDp2 = Math.max(dp1,dp2);
dp0 = newDp0;
dp1 = newDp1;
dp2 = newDp2;
}
return Math.max(dp1,dp2);
}
}
思路:
问题描述:
代码:
class Solution {
public int maxProfit(int[] prices, int fee) {
int dp0=0,dp1=-prices[0];
for(int i=1;i<prices.length;i++){
dp0 = Math.max(dp1+prices[i]-fee,dp0);
dp1 = Math.max(dp0-prices[i],dp1);
}
return dp0<0?0:dp0;
}
}
思路: