-
Notifications
You must be signed in to change notification settings - Fork 5
/
0123.py
22 lines (19 loc) · 940 Bytes
/
0123.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from typing import List
class Solution:
def maxProfit(self, prices: List[int]) -> int:
dp_i_2_0, dp_i_1_0 = 0, 0
# dp_i_2_1, dp_i_1_1 = -prices[0], -prices[0] # 会报错:list index out of range
dp_i_2_1, dp_i_1_1 = float('-inf'), float('-inf') # 负无穷
for i in range(len(prices)):
# 昨天没有股票,昨天有股票今天卖出
dp_i_2_0 = max(dp_i_2_0, dp_i_2_1 + prices[i])
# 昨天有股票,昨天没有股票今天买入
dp_i_2_1 = max(dp_i_2_1, dp_i_1_0 - prices[i])
# 昨天没有股票,昨天有股票今天卖出
dp_i_1_0 = max(dp_i_1_0, dp_i_1_1 + prices[i])
# 昨天有股票,昨天没有股票今天买入
dp_i_1_1 = max(dp_i_1_1, -prices[i])
return dp_i_2_0
if __name__ == "__main__":
prices = [3,3,5,0,0,3,1,4]
print(Solution().maxProfit(prices))