We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7cef253 commit 71f1410Copy full SHA for 71f1410
problems/0121.买卖股票的最佳时机.md
@@ -249,11 +249,14 @@ class Solution {
249
// 所以我们从 i=1 开始遍历数组,一共有 prices.length 天,
250
// 所以是 i<=prices.length
251
for (int i = 1; i <= prices.length; i++) {
252
- int temp = dp[0];
253
// 前一天持有;或当天买入
254
- dp[0] = Math.max(temp, -prices[i - 1]);
+ dp[0] = Math.max(dp[0], -prices[i - 1]);
+ // 如果 dp[0] 被更新,那么 dp[1] 肯定会被更新为正数的 dp[1]
255
+ // 而不是 dp[0]+prices[i-1]==0 的0,
256
+ // 所以这里使用会改变的dp[0]也是可以的
257
+ // 当然 dp[1] 初始值为 0 ,被更新成 0 也没影响
258
// 前一天卖出;或当天卖出, 当天要卖出,得前一天持有才行
- dp[1] = Math.max(dp[1], temp + prices[i - 1]);
259
+ dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);
260
}
261
return dp[1];
262
0 commit comments