Skip to content

Commit 8921868

Browse files
committed
update 714 309
1 parent 48f2c43 commit 8921868

6 files changed

+77
-54
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This is an open-source project that is continually updated
1616

1717
**Total:** 433 problems
1818

19-
**Updated:** 2024-09-10 11:00:40
19+
**Updated:** 2024-09-10 15:17:59
2020

2121
## 链接/Links
2222

@@ -179,7 +179,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
179179
| 2024-06 | [Leetcode-680. Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/description/) | [c++](./leetcode/680.valid-palindrome-ii.cpp), [python3](./leetcode/680.valid-palindrome-ii.py) | Greedy | O\(N\) | O\(1\) | - |
180180
| 2024-07 | [Leetcode-646. Maximum Length Of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) | [c++](./leetcode/646.maximum-length-of-pair-chain.cpp), [python3](./leetcode/646.maximum-length-of-pair-chain.py) | Greedy | O\(NlogN\) | O\(N\) | - |
181181
| 2024-07 | [Leetcode-376. Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/description/) | [c++](./leetcode/376.wiggle-subsequence.cpp), [python3](./leetcode/376.wiggle-subsequence.py) | Greedy | O\(N\) | O\(N\) | - |
182-
| 2024-07 | [Leetcode-714. Best Time To Buy And Sell Stock With Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) | [c++](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp), [python3](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py) | Greedy | O\(N\) | O\(N\) | - |
183182
| 2024-08 | [Leetcode-769. Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/description/) | [c++](./leetcode/769.max-chunks-to-make-sorted.cpp), [python3](./leetcode/769.max-chunks-to-make-sorted.py) | Greedy | O\(N\) | O\(1\) | - |
184183
| 2024-08 | [Leetcode-435. Non Overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) | [c++](./leetcode/435.non-overlapping-intervals.cpp), [python3](./leetcode/435.non-overlapping-intervals.py) | Greedy | O\(NlogN\) | O\(1\) | - |
185184
| 2024-08 | [Leetcode-455. Assign Cookies](https://leetcode.com/problems/assign-cookies/description/) | [c++](./leetcode/455.assign-cookies.cpp), [python3](./leetcode/455.assign-cookies.py) | Greedy | O\(NlogN\) | O\(1\) | - |
@@ -189,6 +188,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
189188
| 2024-08 | [Lintcode-3672. Connecting Cities With Minimum Cost](https://www.lintcode.com/problem/connecting-cities-with-minimum-cost) | [c++](./lintcode/3672.connecting-cities-with-minimum-cost.cpp), [python3](./lintcode/3672.connecting-cities-with-minimum-cost.py) | Greedy | O\(ElogE\) | O\(E\+V\) | - |
190189
| 2024-09 | [Leetcode-122. Best Time To Buy And Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) | [c++](./leetcode/122.best-time-to-buy-and-sell-stock-ii.cpp), [python3](./leetcode/122.best-time-to-buy-and-sell-stock-ii.py) | Greedy | O\(N\) | O\(1\) | - |
191190
| 2024-09 | [Leetcode-860. Lemonade Change](https://leetcode.com/problems/lemonade-change/description/) | [c++](./leetcode/860.lemonade-change.cpp), [python3](./leetcode/860.lemonade-change.py) | Greedy | O\(N\) | O\(1\) | - |
191+
| 2024-09 | [Leetcode-714. Best Time To Buy And Sell Stock With Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) | [c++](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp), [python3](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py) | Greedy | O\(N\) | O\(N\) | - |
192192

193193
## Dynamic Programming
194194

@@ -217,16 +217,13 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
217217
| 2024-07 | [Leetcode-72. Edit Distance](https://leetcode.com/problems/edit-distance/description/) | [c++](./leetcode/72.edit-distance.cpp), [python3](./leetcode/72.edit-distance.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
218218
| 2024-07 | [Leetcode-650. 2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/description/) | [c++](./leetcode/650.2-keys-keyboard.cpp), [python3](./leetcode/650.2-keys-keyboard.py) | Dynamic Programming | \- | \- | - |
219219
| 2024-07 | [Leetcode-10. Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/description/) | [c++](./leetcode/10.regular-expression-matching.cpp), [python3](./leetcode/10.regular-expression-matching.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
220-
| 2024-07 | [Leetcode-188. Best Time To Buy And Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | [c++](./leetcode/188.best-time-to-buy-and-sell-stock-iv.cpp), [python3](./leetcode/188.best-time-to-buy-and-sell-stock-iv.py) | Dynamic Programming | O\(NK\) | O\(NK\) | - |
221-
| 2024-07 | [Leetcode-309. Best Time To Buy And Sell Stock With Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) | [c++](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.cpp), [python3](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
222220
| 2024-07 | [Leetcode-213. House Robber II](https://leetcode.com/problems/house-robber-ii/description/) | [c++](./leetcode/213.house-robber-ii.cpp), [python3](./leetcode/213.house-robber-ii.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
223221
| 2024-07 | [Leetcode-53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/description/) | [c++](./leetcode/53.maximum-subarray.cpp), [python3](./leetcode/53.maximum-subarray.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
224222
| 2024-07 | [Leetcode-343. Integer Break](https://leetcode.com/problems/integer-break/description/) | [c++](./leetcode/343.integer-break.cpp), [python3](./leetcode/343.integer-break.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
225223
| 2024-07 | [Leetcode-583. Delete Operation For Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [c++](./leetcode/583.delete-operation-for-two-strings.cpp), [python3](./leetcode/583.delete-operation-for-two-strings.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
226224
| 2024-07 | [Leetcode-646. Maximum Length Of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) | [c++](./leetcode/646.maximum-length-of-pair-chain.cpp), [python3](./leetcode/646.maximum-length-of-pair-chain.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | - |
227225
| 2024-07 | [Leetcode-376. Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/description/) | [c++](./leetcode/376.wiggle-subsequence.cpp), [python3](./leetcode/376.wiggle-subsequence.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
228226
| 2024-07 | [Leetcode-494. Target Sum](https://leetcode.com/problems/target-sum/description/) | [c++](./leetcode/494.target-sum.cpp), [python3](./leetcode/494.target-sum.py) | Dynamic Programming | O\(N \* Sum\) | O\(N \* Sum\) | - |
229-
| 2024-07 | [Leetcode-714. Best Time To Buy And Sell Stock With Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) | [c++](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp), [python3](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
230227
| 2024-07 | [Leetcode-241. Different Ways To Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) | [c++](./leetcode/241.different-ways-to-add-parentheses.cpp), [python3](./leetcode/241.different-ways-to-add-parentheses.py) | Dynamic Programming | \- | \- | - |
231228
| 2024-07 | [Leetcode-312. Burst Balloons](https://leetcode.com/problems/burst-balloons/description/) | [c++](./leetcode/312.burst-balloons.cpp), [python3](./leetcode/312.burst-balloons.py) | Dynamic Programming | O\(N^3\) | O\(N^2\) | - |
232229
| 2024-07 | [Leetcode-338. Counting Bits](https://leetcode.com/problems/counting-bits/description/) | [c++](./leetcode/338.counting-bits.cpp), [python3](./leetcode/338.counting-bits.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
@@ -243,6 +240,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
243240
| 2024-09 | [Leetcode-152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/description/) | [c++](./leetcode/152.maximum-product-subarray.cpp), [python3](./leetcode/152.maximum-product-subarray.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
244241
| 2024-09 | [Leetcode-121. Best Time To Buy And Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) | [c++](./leetcode/121.best-time-to-buy-and-sell-stock.cpp), [python3](./leetcode/121.best-time-to-buy-and-sell-stock.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
245242
| 2024-09 | [Leetcode-123. Best Time To Buy And Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/) | [c++](./leetcode/123.best-time-to-buy-and-sell-stock-iii.cpp), [python3](./leetcode/123.best-time-to-buy-and-sell-stock-iii.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
243+
| 2024-09 | [Leetcode-188. Best Time To Buy And Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | [c++](./leetcode/188.best-time-to-buy-and-sell-stock-iv.cpp), [python3](./leetcode/188.best-time-to-buy-and-sell-stock-iv.py) | Dynamic Programming | O\(NK\) | O\(NK\) | - |
244+
| 2024-09 | [Leetcode-714. Best Time To Buy And Sell Stock With Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) | [c++](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp), [python3](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
245+
| 2024-09 | [Leetcode-309. Best Time To Buy And Sell Stock With Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) | [c++](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.cpp), [python3](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
246246

247247
## Binary Search
248248

leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,36 @@ class Solution {
3535
int maxProfit(vector<int>& prices) {
3636
int n = prices.size();
3737

38-
vector<int> rest(n, 0);
39-
vector<int> buy(n, 0);
40-
vector<int> sell(n, 0);
41-
42-
buy[0] = -prices[0];
43-
for (int i = 1; i < n; i++) {
44-
buy[i] = max(buy[i - 1], rest[i - 1] - prices[i]);
45-
sell[i] = buy[i] + prices[i];
46-
rest[i] = max(rest[i - 1], sell[i - 1]);
38+
vector<int> buy(n + 1, INT_MIN);
39+
vector<int> sell(n + 1, 0);
40+
vector<int> rest(n + 1, 0);
41+
42+
for (int i = 1; i <= n; i++) {
43+
int p = prices[i - 1];
44+
buy[i] = max(buy[i - 1], rest[i - 1] - p);
45+
rest[i] = sell[i - 1];
46+
sell[i] = max(sell[i - 1], buy[i] + p);
47+
}
48+
49+
return sell[n];
50+
}
51+
};
52+
53+
class Solution {
54+
public:
55+
int maxProfit(vector<int>& prices) {
56+
int n = prices.size();
57+
58+
int buy = INT_MIN;
59+
int sell = 0;
60+
int rest = 0;
61+
62+
for (auto p: prices) {
63+
buy = max(buy, rest - p);
64+
rest = sell;
65+
sell = max(sell, buy + p);
4766
}
4867

49-
return max(rest[n - 1], sell[n - 1]);
68+
return sell;
5069
}
5170
};

leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,29 @@
3232

3333
class Solution:
3434
def maxProfit(self, prices: List[int]) -> int:
35+
n = len(prices)
36+
buy = [float('-inf') for i in range(n + 1)]
37+
sell = [0 for i in range(n + 1)]
38+
rest = [0 for i in range(n + 1)]
39+
40+
for i in range(1, n + 1):
41+
p = prices[i - 1]
42+
buy[i] = max(buy[i - 1], rest[i - 1] - p)
43+
rest[i] = sell[i - 1]
44+
sell[i] = max(sell[i - 1], buy[i] + p)
3545

46+
return sell[n]
47+
48+
class Solution:
49+
def maxProfit(self, prices: List[int]) -> int:
3650
n = len(prices)
37-
rest = [0 for i in range(n)]
38-
buy = [0 for i in range(n)]
39-
sell = [0 for i in range(n)]
51+
buy = float('-inf')
52+
sell = 0
53+
rest = 0
54+
55+
for p in prices:
56+
buy = max(buy, rest - p)
57+
rest = sell
58+
sell = max(sell, buy + p)
4059

41-
buy[0] = -prices[0]
42-
for i in range(1, n):
43-
rest[i] = max(rest[i - 1], sell[i - 1])
44-
buy[i] = max(buy[i - 1], rest[i - 1] - prices[i])
45-
sell[i] = buy[i] + prices[i]
46-
47-
return max(rest[n - 1], sell[n - 1])
60+
return sell

leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ class Solution {
5858
class Solution {
5959
public:
6060
int maxProfit(vector<int>& prices, int fee) {
61-
int n = prices.size();
6261
int buy = INT_MIN;
6362
int sell = 0;
64-
65-
for (int i = 1; i <= n; i++) {
66-
int next_buy = max(buy, sell - prices[i - 1]);
67-
int next_sell = (buy == INT_MIN) ? sell : max(sell, buy + prices[i - 1] - fee);
68-
buy = next_buy;
69-
sell = next_sell;
63+
for (auto p: prices) {
64+
buy = max(buy, sell - p);
65+
sell = max(sell, buy + p - fee);
7066
}
7167

7268
return sell;

leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ def maxProfit(self, prices: List[int], fee: int) -> int:
5252

5353
class Solution:
5454
def maxProfit(self, prices: List[int], fee: int) -> int:
55-
n = len(prices)
5655
buy = float('-inf')
5756
sell = 0
5857

59-
for i in range(1, n + 1):
60-
next_buy = max(buy, sell - prices[i - 1])
61-
next_sell = max(sell, next_buy + prices[i - 1] - fee)
58+
for p in prices:
59+
buy = max(buy, sell - p)
60+
sell = max(sell, buy + p - fee)
6261

63-
buy, sell = next_buy, next_sell
64-
65-
return sell
62+
return sell
63+

test/python.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55

66
from collections import defaultdict
77
class Solution:
8-
def maxProduct(self, nums: List[int]) -> int:
9-
n = len(nums)
10-
min_pre = nums[0]
11-
max_pre = nums[0]
12-
res = nums[0]
8+
def maxProfit(self, prices: List[int]) -> int:
9+
n = len(prices)
10+
buy = [float('-inf') for i in range(n + 1)]
11+
sell = [0 for i in range(n + 1)]
12+
rest = [0 for i in range(n + 1)]
1313

14-
for i in range(1, n):
15-
if nums[i] >= 0:
16-
max_pre = max(nums[i], nums[i] * max_pre)
17-
min_pre = min(nums[i], nums[i] * min_pre)
18-
else:
19-
max_pre, min_pre = max(nums[i], nums[i] * min_pre), min(nums[i], nums[i] * max_pre)
14+
for i in range(1, n + 1):
15+
p = prices[i - 1]
16+
buy[i] = max(buy[i - 1], rest[i - 1] - p)
17+
rest[i] = max(rest[i - 1], sell[i - 1])
18+
sell[i] = max(sell[i - 1], buy[i] + p)
2019

21-
res = max(res, max_pre)
22-
23-
return res
20+
return sell[n]
2421

2522
s = Solution()
26-
res = s.maxProduct([-4,-3,-2])
23+
res = s.maxProfit([1,2,3,0,2])
2724
print(res)

0 commit comments

Comments
 (0)