Skip to content

Commit 032bc7e

Browse files
Miracleminlabuladong
authored andcommitted
Python3
1 parent 7d97747 commit 032bc7e

File tree

1 file changed

+79
-2
lines changed

1 file changed

+79
-2
lines changed

动态规划系列/团灭股票问题.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,86 @@ int maxProfit_k_any(int max_k, int[] prices) {
391391
**致力于把算法讲清楚!欢迎关注我的微信公众号 labuladong,查看更多通俗易懂的文章**
392392

393393
![labuladong](../pictures/labuladong.png)
394-
394+
[Hanmin](https://github.com/Miraclemin/) 提供 Python3 代码:
395+
**第一题,k = 1**
396+
```python
397+
def maxProfit(self, prices: List[int]) -> int:
398+
dp_i_0,dp_i_1 = 0,float('-inf')
399+
for price in prices:
400+
dp_i_0 = max(dp_i_0, dp_i_1 + price)
401+
dp_i_1 = max(dp_i_1, -price)
402+
return dp_i_0
403+
```
404+
**第二题,k = +infinity**
405+
```python
406+
def maxProfit_k_inf(self, prices: List[int]) -> int:
407+
dp_i_0,dp_i_1 = 0,float('-inf')
408+
for price in prices:
409+
temp = dp_i_0
410+
dp_i_0 = max(dp_i_0, dp_i_1 + price)
411+
dp_i_1 = max(dp_i_1, temp - price)
412+
return dp_i_0
413+
```
414+
**第三题,k = +infinity with cooldown**
415+
```python
416+
def maxProfit_with_cool(self, prices: List[int]) -> int:
417+
dp_i_0,dp_i_1 = 0,float('-inf')
418+
dp_pre_0 = 0 ##代表 dp[i-2][0]
419+
for price in prices:
420+
temp = dp_i_0
421+
dp_i_0 = max(dp_i_0, dp_i_1 + price)
422+
dp_i_1 = max(dp_i_1, dp_pre_0 - price)
423+
dp_pre_0 = temp
424+
return dp_i_0
425+
```
426+
**第四题,k = +infinity with fee**
427+
```python
428+
def maxProfit_with_fee(self, prices: List[int], fee: int) -> int:
429+
dp_i_0,dp_i_1 = 0,float('-inf')
430+
for price in prices:
431+
temp = dp_i_0
432+
dp_i_0 = max(dp_i_0, dp_i_1 + price)
433+
dp_i_1 = max(dp_i_1, temp - price -fee)
434+
return dp_i_0
435+
```
436+
**第五题,k = 2**
437+
```python
438+
def maxProfit_k_2(self, prices: List[int]) -> int:
439+
dp_i10,dp_i11 = 0,float('-inf')
440+
dp_i20,dp_i21 = 0,float('-inf')
441+
for price in prices:
442+
dp_i20 = max(dp_i20, dp_i21 + price)
443+
dp_i21 = max(dp_i21, dp_i10 - price)
444+
dp_i10 = max(dp_i10, dp_i11 + price)
445+
dp_i11 = max(dp_i11, -price)
446+
return dp_i20
447+
```
448+
**第六题,k = any integer**
449+
```python
450+
def maxProfit_k_any(self, max_k: int, prices: List[int]) -> int:
451+
n = len(prices)
452+
if max_k > n // 2:
453+
return self.maxProfit_k_inf(prices)
454+
else:
455+
dp = [[[None, None] for _ in range(max_k + 1)] for _ in range(n)]
456+
for i in range(0,n):
457+
for k in range(max_k,0,-1):
458+
if i-1 == -1:## 处理 base case
459+
dp[i][k][0] = 0
460+
## 解释:
461+
## dp[i][k][0] = max(dp[-1][k][0], dp[-1][k][1] + prices[i])
462+
## = max(0, -infinity + prices[i]) = 0
463+
dp[i][k][1] = -prices[i]
464+
## 解释:
465+
## dp[i][1] = max(dp[-1][k][1], dp[-1][k][0] - prices[i])
466+
## = max(-infinity, 0 - prices[i]) = -prices[i]
467+
continue
468+
dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
469+
dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i])
470+
return dp[n - 1][max_k][0];
471+
```
395472
[上一篇:动态规划之KMP字符匹配算法](../动态规划系列/动态规划之KMP字符匹配算法.md)
396473

397474
[下一篇:团灭 LeetCode 打家劫舍问题](../动态规划系列/抢房子.md)
398475

399-
[目录](../README.md#目录)
476+
[目录](../README.md#目录)

0 commit comments

Comments
 (0)