Skip to content

Commit

Permalink
增加了122优化空间的java题解
Browse files Browse the repository at this point in the history
  • Loading branch information
spacker-343 committed Nov 24, 2021
1 parent 71f1410 commit eacf15d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions problems/0122.买卖股票的最佳时机II.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ class Solution { // 动态规划
}
```

```java
// 优化空间
class Solution {
public int maxProfit(int[] prices) {
int[] dp=new int[2];
// 0表示持有,1表示卖出
dp[0]=-prices[0];
dp[1]=0;
for(int i=1; i<=prices.length; i++){
// 前一天持有; 或当天卖出然后买入
dp[0]=Math.max(dp[0], dp[1]-prices[i-1]);
// 前一天卖出; 或当天卖出,当天卖出,得先持有
dp[1]=Math.max(dp[1], dp[0]+prices[i-1]);
}
return dp[1];
}
}
```



### Python
Expand Down

0 comments on commit eacf15d

Please sign in to comment.