一段时间没做题了,发现真的没有天赋
题意
- 当天未交易的结果dp[j - 1]
- 当天完成交易的结果prices[j] - prices[i] + dp[i-2]
- 非i天买入,j天时的结果dp[j]
- 3种情况取最大值
class Solution { public int maxProfit(int[] prices) { if (prices.length < 2) { return 0; } int dp[] = new int[prices.length]; for (int i = 0; i < prices.length - 1; i++) { for (int j = i + 1; j < prices.length; j++) { dp[j] = Math.max(Math.max(dp[j], dp[j - 1]), (prices[j] - prices[i]) + (i > 1?dp[i-2]:0)); } } return dp[prices.length - 1]; } }