class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length + 1][2];
dp[0][0] = Integer.MIN_VALUE;
for(int i = 1;i < prices.length + 1;i++){
int price = prices[i - 1];
dp[i][0] = Math.max(dp[i - 1][0],-price);
if(dp[i][0] + price > 0){
dp[i][1] = dp[i - 1][1] + dp[i][0] + price;
dp[i][0] = -price;
}else {
dp[i][1] = dp[i - 1][1];
}
}
return dp[prices.length][1];
}
}
122. 买卖股票的最佳时机 II
最新推荐文章于 2024-06-19 15:09:37 发布