不小心添加到714(贪心)哪里去了,现在把一维数组优化空间的代码重添加进了动态规划题解中,已更正

This commit is contained in:
LiHua
2021-11-24 22:11:33 +08:00
parent c73bedb0da
commit 09a19372ba
2 changed files with 14 additions and 16 deletions

View File

@ -195,22 +195,6 @@ class Solution { // 动态规划
}
```
```java
// 一维数组优化
class Solution {
public int maxProfit(int[] prices, int fee) {
int[] dp = new int[2];
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] - fee);
}
return dp[1];
}
}
```
Python

View File

@ -134,6 +134,20 @@ public int maxProfit(int[] prices, int fee) {
}
return Math.max(dp[len - 1][0], dp[len - 1][1]);
}
// 一维数组优化
class Solution {
public int maxProfit(int[] prices, int fee) {
int[] dp = new int[2];
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] - fee);
}
return dp[1];
}
}
```
Python