Skip to content

Commit 65afe2f

Browse files
committed
update 188
1 parent c4e4e5d commit 65afe2f

5 files changed

+118
-123
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
88
Total sovled: **342**<br/>
9-
Auto updated at: **2024-07-17 12:10:07**<br/>
9+
Auto updated at: **2024-07-17 17:42:57**<br/>
1010

1111
## 软件/Softwares
1212
- [Anki](https://apps.ankiweb.net/)
@@ -195,7 +195,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
195195
| [Leetcode-3048. Earliest Second To Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/description/) | [python](./leetcode/3048.earliest-second-to-mark-indices-i.py), [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp) | O\(M\*logM\) | O\(M\) | \- | - |
196196

197197
## Dynamic Programming
198-
| Problem(27) | Solution | Time | Space | Note | Ref |
198+
| Problem(28) | Solution | Time | Space | Note | Ref |
199199
| ----- | ----- | ----- | ----- | ----- | ----- |
200200
| [Leetcode-10. Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/description/) | [python](./leetcode/10.regular-expression-matching.py), [c++](./leetcode/10.regular-expression-matching.cpp) | O\(MN\) | O\(MN\) | \- | - |
201201
| [Leetcode-64. Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/description/) | [c++](./leetcode/64.minimum-path-sum.cpp), [python](./leetcode/64.minimum-path-sum.py) | O\(MN\) | O\(MN\) | \- | - |
@@ -206,6 +206,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
206206
| [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), [python](./leetcode/121.best-time-to-buy-and-sell-stock.py) | O\(N\) | O\(1\) | \- | - |
207207
| [Leetcode-122. Best Time To Buy And Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) | [python](./leetcode/122.best-time-to-buy-and-sell-stock-ii.py), [c++](./leetcode/122.best-time-to-buy-and-sell-stock-ii.cpp) | O\(N\) | O\(1\) | \- | - |
208208
| [Leetcode-139. Word Break](https://leetcode.com/problems/word-break/description/) | [c++](./leetcode/139.word-break.cpp), [python](./leetcode/139.word-break.py) | O\(MN\) | O\(N\) | \- | - |
209+
| [Leetcode-188. Best Time To Buy And Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | [python](./leetcode/188.best-time-to-buy-and-sell-stock-iv.py), [c++](./leetcode/188.best-time-to-buy-and-sell-stock-iv.cpp) | O\(NK\) | O\(NK\) | \- | - |
209210
| [Leetcode-198. House Robber](https://leetcode.com/problems/house-robber/description/) | [c++](./leetcode/198.house-robber.cpp), [python](./leetcode/198.house-robber.py) | O\(N\) | O\(N\) | \- | - |
210211
| [Leetcode-221. Maximal Square](https://leetcode.com/problems/maximal-square/description/) | [python](./leetcode/221.maximal-square.py), [c++](./leetcode/221.maximal-square.cpp) | O\(MN\) | O\(MN\) | \- | - |
211212
| [Leetcode-279. Perfect Squares](https://leetcode.com/problems/perfect-squares/description/) | [c++](./leetcode/279.perfect-squares.cpp), [python](./leetcode/279.perfect-squares.py) | O\(n^\{3/2\}\) | O\(N\) | \- | - |
@@ -328,7 +329,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
328329
| [Lintcode-1790. Rotate String II](https://www.lintcode.com/problem/rotate-string-ii) | [c++](./lintcode/1790.rotate-string-ii.cpp), [python](./lintcode/1790.rotate-string-ii.py) | O\(N\) | O\(N\) | Simulation | - |
329330

330331
## Other
331-
| Problem(238) | Solution | Time | Space | Note | Ref |
332+
| Problem(237) | Solution | Time | Space | Note | Ref |
332333
| ----- | ----- | ----- | ----- | ----- | ----- |
333334
| [Leetcode-1. Two Sum](https://leetcode.com/problems/two-sum/description/) | [python](./leetcode/1.two-sum.py), [c++](./leetcode/1.two-sum.cpp) | \- | \- | \- | - |
334335
| [Leetcode-2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [python](./leetcode/2.add-two-numbers.py), [c++](./leetcode/2.add-two-numbers.cpp) | \- | \- | \- | - |
@@ -416,7 +417,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
416417
| [Leetcode-169. Majority Element](https://leetcode.com/problems/majority-element/description/) | [python](./leetcode/169.majority-element.py), [c++](./leetcode/169.majority-element.cpp) | \- | \- | \- | - |
417418
| [Leetcode-170. Two Sum Iii Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) | [python](./leetcode/170.two-sum-iii-data-structure-design.py), [c++](./leetcode/170.two-sum-iii-data-structure-design.cpp) | \- | \- | \- | - |
418419
| [Leetcode-175. Combine Two Tables](https://leetcode.com/problems/combine-two-tables/description/) | [sql](./leetcode/175.combine-two-tables.sql) | \- | \- | \- | - |
419-
| [Leetcode-188. Best Time To Buy And Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | [python](./leetcode/188.best-time-to-buy-and-sell-stock-iv.py), [c++](./leetcode/188.best-time-to-buy-and-sell-stock-iv.cpp) | \- | \- | \- | - |
420420
| [Leetcode-200. Number Of Islands](https://leetcode.com/problems/number-of-islands/description/) | [c++](./leetcode/200.number-of-islands.cpp), [python](./leetcode/200.number-of-islands.py) | \- | \- | \- | - |
421421
| [Leetcode-202. Happy Number](https://leetcode.com/problems/happy-number/description/) | [python](./leetcode/202.happy-number.py), [c++](./leetcode/202.happy-number.cpp) | \- | \- | \- | - |
422422
| [Leetcode-203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/description/) | [python](./leetcode/203.remove-linked-list-elements.py) | \- | \- | \- | - |

leetcode/121.best-time-to-buy-and-sell-stock.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
class Solution {
3333
public:
3434
int maxProfit(vector<int>& prices) {
35-
int buy = INT_MAX;
35+
int buy = INT_MIN;
3636
int sell = 0;
3737

3838
for (int p: prices) {
39-
buy = min(buy, p);
40-
sell = max(sell, p - buy);
39+
buy = max(buy, 0 - p);
40+
sell = max(sell, p + buy);
4141
}
4242

4343
return sell;

leetcode/121.best-time-to-buy-and-sell-stock.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131

3232
class Solution:
3333
def maxProfit(self, prices: List[int]) -> int:
34-
buy = float('inf')
34+
buy = float('-inf')
3535
sell = 0
3636

3737
for i in range(len(prices)):
38-
buy = min(buy, prices[i])
39-
sell = max(sell, prices[i] - buy)
38+
buy = max(buy, 0 - prices[i])
39+
sell = max(sell, prices[i] + buy)
4040

4141

4242
return sell
Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,66 @@
1+
// Tag: Array, Dynamic Programming
2+
// Time: O(NK)
3+
// Space: O(NK)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.
8+
// Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times.
9+
// Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
10+
//  
11+
// Example 1:
12+
//
13+
// Input: k = 2, prices = [2,4,1]
14+
// Output: 2
15+
// Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
16+
//
17+
// Example 2:
18+
//
19+
// Input: k = 2, prices = [3,2,6,5,0,3]
20+
// Output: 7
21+
// Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
22+
//
23+
//  
24+
// Constraints:
25+
//
26+
// 1 <= k <= 100
27+
// 1 <= prices.length <= 1000
28+
// 0 <= prices[i] <= 1000
29+
//
30+
//
31+
132
class Solution {
233
public:
334
int maxProfit(int k, vector<int>& prices) {
4-
5-
if (k == 0 || prices.size() == 0)
6-
{
35+
if (prices.size() < 2) {
736
return 0;
837
}
9-
10-
int m = prices.size();
11-
int res = 0;
12-
13-
if (k > m / 2) // same as stock 2
14-
{
15-
for(auto i = 1; i < m; ++i)
16-
{
17-
if (prices[i] - prices[i - 1] > 0)
18-
{
19-
res += (prices[i] - prices[i - 1]);
20-
}
21-
}
38+
39+
if (k >= prices.size()) {
40+
return maxProfitUnlimited(prices);
2241
}
23-
else // same as stock 3
24-
{
25-
int state = 2 * k + 1;
26-
vector<vector<int>> table(m + 1, vector<int>(state + 1, 0));
27-
// init
28-
for (auto j = 1; j <= state; ++j)
29-
{
30-
table[0][j] = (j == 1) ? 0 : INT_MIN;
31-
}
32-
33-
for (auto i = 1; i <= m; ++i)
34-
{
35-
for (auto j = 1; j <= state; ++j)
36-
{
37-
int value = 0;
38-
if (j % 2 == 1) // 1, 3, 5 ...
39-
{
40-
// f[i][j] = max(f[i - 1][j], f[i - 1][j - 1] + prices[i - 1] - prices[i - 2])
41-
value = table[i - 1][j];
42-
if (i - 2 >= 0 && j > 1 && table[i - 1][j - 1] != INT_MIN)
43-
{
44-
value = max(value, table[i - 1][j - 1] + prices[i - 1] - prices[i - 2]);
45-
}
46-
}
47-
else // 2, 4, 6 ...
48-
{
49-
// f[i][j] = max(f[i - 1][j - 1], f[i - 1][j] + prices[i - 1] - prices[i - 2], f[i - 1][j - 2] + prices[i - 1] - prices[i - 2])
50-
value = table[i - 1][j - 1];
51-
if (i - 2 >= 0 && table[i - 1][j] != INT_MIN)
52-
{
53-
value = max(value, table[i - 1][j] + prices[i - 1] - prices[i - 2]);
54-
}
55-
56-
if (i - 2 >= 0 && j > 2 && table[i - 1][j - 2] != INT_MIN)
57-
{
58-
value = max(value, table[i - 1][j - 2] + prices[i - 1] - prices[i - 2]);
59-
}
60-
}
61-
table[i][j] = value;
62-
}
42+
43+
vector<int> buy(k + 1, INT_MIN);
44+
vector<int> sell(k + 1, 0);
45+
46+
for (int p : prices) {
47+
for (int i = 1; i <= k; i++) {
48+
buy[i] = max(buy[i], sell[i - 1] - p);
49+
sell[i] = max(sell[i], buy[i] + p);
6350
}
64-
65-
for (auto j = 1; j <= state; j+=2) {
66-
res = max(res, table[m][j]);
51+
}
52+
53+
return sell[k];
54+
}
55+
56+
int maxProfitUnlimited(vector<int>& prices) {
57+
int profit = 0;
58+
for (int i = 1; i < prices.size(); i++) {
59+
if (prices[i] - prices[i - 1] > 0) {
60+
profit += prices[i] - prices[i - 1];
6761
}
6862
}
69-
70-
return res;
63+
64+
return profit;
7165
}
7266
};
Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
1-
class Solution(object):
2-
def maxProfit(self, k, prices):
3-
"""
4-
:type k: int
5-
:type prices: List[int]
6-
:rtype: int
7-
"""
8-
if prices is None or len(prices) == 0 or k == 0:
9-
return 0
10-
11-
res = 0
12-
m = len(prices)
13-
14-
if k > m // 2: # same as stock 2
15-
for i in range(1, m):
16-
if prices[i] - prices[i - 1] > 0:
17-
res += prices[i] - prices[i - 1]
18-
19-
else: # same as stock 3
20-
state = 2 * k + 1
21-
table = [[0 for _ in range(state + 1)] for _ in range(m + 1)]
22-
23-
# init
24-
for j in range(1, state + 1):
25-
table[0][j] = 0 if j == 1 else float('-inf')
26-
27-
for i in range(1, m + 1):
28-
for j in range(1, state + 1):
29-
value = 0
30-
if j % 2 == 1: # 1, 3, 5
31-
# f[i][j] = max(f[i - 1][j], f[i - 1][j - 1] + prices[i - 1] - prices[i - 2])
32-
value = table[i - 1][j]
33-
34-
if i - 2 >= 0 and j > 1 and table[i - 1][j - 1] != float('-inf'):
35-
value = max(value, table[i - 1][j - 1] + prices[i - 1] - prices[i - 2])
36-
37-
else: # 2, 4, 6
38-
# f[i][j] = max(f[i - 1][j - 1], f[i - 1][j] + prices[i - 1] - prices[i - 2], f[i - 1][j - 2] + prices[i - 1] - prices[i - 2])
39-
value = table[i - 1][j - 1]
40-
41-
if i - 2 >= 0 and table[i - 1][j] != float('-inf'):
42-
value = max(value, table[i - 1][j] + prices[i - 1] - prices[i - 2])
43-
44-
if i - 2 >= 0 and j > 2 and table[i - 1][j - 2] != float('-inf'):
45-
value = max(value, table[i - 1][j - 2] + prices[i - 1] - prices[i - 2])
46-
47-
table[i][j] = value
48-
49-
for j in range(1, state + 1, 2):
50-
res = max(res, table[m][j])
51-
52-
return res
1+
# Tag: Array, Dynamic Programming
2+
# Time: O(NK)
3+
# Space: O(NK)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.
8+
# Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times.
9+
# Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
10+
#  
11+
# Example 1:
12+
#
13+
# Input: k = 2, prices = [2,4,1]
14+
# Output: 2
15+
# Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
16+
#
17+
# Example 2:
18+
#
19+
# Input: k = 2, prices = [3,2,6,5,0,3]
20+
# Output: 7
21+
# Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
22+
#
23+
#  
24+
# Constraints:
25+
#
26+
# 1 <= k <= 100
27+
# 1 <= prices.length <= 1000
28+
# 0 <= prices[i] <= 1000
29+
#
30+
#
31+
32+
class Solution:
33+
def maxProfit(self, k: int, prices: List[int]) -> int:
34+
if k >= len(prices):
35+
return self.maxProfitUnlimited(prices)
36+
37+
buy = [float('-inf') for i in range(k + 1)]
38+
sell = [0 for i in range(k + 1)]
39+
for p in prices:
40+
for i in range(1, k + 1):
41+
buy[i] = max(buy[i], sell[i - 1] - p)
42+
sell[i] = max(sell[i], buy[i] + p)
43+
44+
return sell[k]
45+
46+
def maxProfitUnlimited(sekf, prices: List[int]) -> int:
47+
profit = 0
48+
49+
for i in range(1, len(prices)):
50+
if prices[i] > prices[i - 1]:
51+
profit += prices[i] - prices[i - 1]
52+
53+
return profit

0 commit comments

Comments
 (0)