Skip to content

Commit 09a1937

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

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

problems/0714.买卖股票的最佳时机含手续费.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,6 @@ class Solution { // 动态规划
195195
}
196196
```
197197

198-
```java
199-
// 一维数组优化
200-
class Solution {
201-
public int maxProfit(int[] prices, int fee) {
202-
int[] dp = new int[2];
203-
dp[0] = -prices[0];
204-
dp[1] = 0;
205-
for (int i = 1; i <= prices.length; i++) {
206-
dp[0] = Math.max(dp[0], dp[1] - prices[i - 1]);
207-
dp[1] = Math.max(dp[1], dp[0] + prices[i - 1] - fee);
208-
}
209-
return dp[1];
210-
}
211-
}
212-
```
213-
214198

215199

216200
Python:

problems/0714.买卖股票的最佳时机含手续费(动态规划).md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ public int maxProfit(int[] prices, int fee) {
134134
}
135135
return Math.max(dp[len - 1][0], dp[len - 1][1]);
136136
}
137+
138+
// 一维数组优化
139+
class Solution {
140+
public int maxProfit(int[] prices, int fee) {
141+
int[] dp = new int[2];
142+
dp[0] = -prices[0];
143+
dp[1] = 0;
144+
for (int i = 1; i <= prices.length; i++) {
145+
dp[0] = Math.max(dp[0], dp[1] - prices[i - 1]);
146+
dp[1] = Math.max(dp[1], dp[0] + prices[i - 1] - fee);
147+
}
148+
return dp[1];
149+
}
150+
}
137151
```
138152

139153
Python:

0 commit comments

Comments
 (0)