题目链接
动态规划
class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0 || prices.length == 1){
return 0;
}
// 初始化
int[][] dp = new int[prices.length + 1][2];
dp[1][0] = -prices[0];
for(int i = 2; i <= prices.length; i++){
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 2][1] - prices[i - 1]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i - 1]);
}
return dp[prices.length][1];
}
}