Skip to content

Commit 914395f

Browse files
committed
add 309
1 parent 65afe2f commit 914395f

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
8-
Total sovled: **342**<br/>
9-
Auto updated at: **2024-07-17 17:42:57**<br/>
8+
Total sovled: **343**<br/>
9+
Auto updated at: **2024-07-17 19:22:36**<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(28) | Solution | Time | Space | Note | Ref |
198+
| Problem(29) | 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\) | \- | - |
@@ -211,6 +211,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
211211
| [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\) | \- | - |
212212
| [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\) | \- | - |
213213
| [Leetcode-300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/) | [python](./leetcode/300.longest-increasing-subsequence.py), [c++](./leetcode/300.longest-increasing-subsequence.cpp) | O\(N\*logN\) | O\(N\) | LIS \| std::lower\_bound | - |
214+
| [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), [python](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py) | O\(N\) | O\(N\) | \- | - |
214215
| [Leetcode-322. Coin Change](https://leetcode.com/problems/coin-change/description/) | [c++](./leetcode/322.coin-change.cpp), [python](./leetcode/322.coin-change.py) | O\(K \* N\) | O\(N\) | Index | [Video](https://youtu.be/EjMjlFjLRiM) |
215216
| [Leetcode-375. Guess Number Higher Or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/description/) | [c++](./leetcode/375.guess-number-higher-or-lower-ii.cpp), [python](./leetcode/375.guess-number-higher-or-lower-ii.py) | O\(N^3\) | O\(N^2\) | DP\(Index\) | - |
216217
| [Leetcode-413. Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/description/) | [c++](./leetcode/413.arithmetic-slices.cpp), [python](./leetcode/413.arithmetic-slices.py) | O\(N\) | O\(N\) | \- | - |
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Tag: Array, Dynamic Programming
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given an array prices where prices[i] is the price of a given stock on the ith day.
8+
// Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
9+
//
10+
// After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
11+
//
12+
// Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
13+
//  
14+
// Example 1:
15+
//
16+
// Input: prices = [1,2,3,0,2]
17+
// Output: 3
18+
// Explanation: transactions = [buy, sell, cooldown, buy, sell]
19+
//
20+
// Example 2:
21+
//
22+
// Input: prices = [1]
23+
// Output: 0
24+
//
25+
//  
26+
// Constraints:
27+
//
28+
// 1 <= prices.length <= 5000
29+
// 0 <= prices[i] <= 1000
30+
//
31+
//
32+
33+
class Solution {
34+
public:
35+
int maxProfit(vector<int>& prices) {
36+
int n = prices.size();
37+
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]);
47+
}
48+
49+
return max(rest[n - 1], sell[n - 1]);
50+
}
51+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Tag: Array, Dynamic Programming
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given an array prices where prices[i] is the price of a given stock on the ith day.
8+
# Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
9+
#
10+
# After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
11+
#
12+
# Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
13+
#  
14+
# Example 1:
15+
#
16+
# Input: prices = [1,2,3,0,2]
17+
# Output: 3
18+
# Explanation: transactions = [buy, sell, cooldown, buy, sell]
19+
#
20+
# Example 2:
21+
#
22+
# Input: prices = [1]
23+
# Output: 0
24+
#
25+
#  
26+
# Constraints:
27+
#
28+
# 1 <= prices.length <= 5000
29+
# 0 <= prices[i] <= 1000
30+
#
31+
#
32+
33+
class Solution:
34+
def maxProfit(self, prices: List[int]) -> int:
35+
36+
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)]
40+
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])

0 commit comments

Comments
 (0)