mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
不小心添加到714(贪心)哪里去了,现在把一维数组优化空间的代码重添加进了动态规划题解中,已更正
This commit is contained in:
@ -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:
|
Python:
|
||||||
|
@ -134,6 +134,20 @@ public int maxProfit(int[] prices, int fee) {
|
|||||||
}
|
}
|
||||||
return Math.max(dp[len - 1][0], dp[len - 1][1]);
|
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:
|
Python:
|
||||||
|
Reference in New Issue
Block a user