@@ -940,11 +940,169 @@ class Solution:
940
940
return dp[- 1 ][n]
941
941
```
942
942
943
+ ###[ best-time-to-buy-and-sell-stock] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ )
944
+ 121 . 买卖股票的最佳时机
945
+ 给定一个数组 prices ,它的第 i 个元素 prices[ i] 表示一支给定股票第 i 天的价格。
946
+
947
+ 只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
948
+
949
+ 返回可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
950
+
951
+ ``` Python
952
+ class Solution :
953
+ def maxProfit (self ,prices : List[int ]) ->int :
954
+ buy= float (" inf" )
955
+ sell= 0
956
+ for dayin prices:
957
+ buy= min (buy, day)
958
+ sell= max (sell, day- buy)
959
+ return sell
960
+ ```
961
+
962
+ ###[ best-time-to-buy-and-sell-stock-ii] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ )
963
+ 122 . 买卖股票的最佳时机 II
964
+ 给定一个数组 prices ,其中 prices[ i] 是一支给定股票第 i 天的价格。
965
+
966
+ 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
967
+
968
+ 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
969
+
970
+ ``` Python
971
+ class Solution (object ):
972
+ def maxProfit (self ,prices ):
973
+ """
974
+ :type prices: List[int]
975
+ :rtype: int
976
+ """
977
+ length= len (prices)
978
+ dp= [[0 ,0 ]for _in range (length)]
979
+ dp[0 ][0 ]= 0
980
+ dp[0 ][1 ]= - prices[0 ]
981
+ for iin range (1 , length):
982
+ dp[i][0 ]= max (dp[i- 1 ][0 ],dp[i- 1 ][1 ]+ prices[i])
983
+ dp[i][1 ]= max (dp[i- 1 ][0 ]- prices[i],dp[i- 1 ][1 ])
984
+ return max (dp[- 1 ][0 ],dp[- 1 ][1 ])
985
+ ```
986
+
987
+ ``` Python
988
+ class Solution :
989
+ def maxProfit (self ,prices : List[int ]) ->int :
990
+ profit= 0
991
+ for iin range (1 ,len (prices)):
992
+ tmp= prices[i]- prices[i- 1 ]
993
+ if tmp> 0 : profit+= tmp
994
+ return profit
995
+ ```
996
+
997
+
998
+ ###[ best-time-to-buy-and-sell-stock-iii] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/ )
999
+
1000
+ 123 . 买卖股票的最佳时机 III
1001
+ 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
1002
+
1003
+ 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
1004
+
1005
+ 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
1006
+
1007
+
1008
+ ``` Python
1009
+ class Solution :
1010
+ def maxProfit (self ,prices : List[int ]) ->int :
1011
+ n= len (prices)
1012
+ buy1= buy2= - prices[0 ]
1013
+ sell1= sell2= 0
1014
+ for iin range (1 , n):
1015
+ buy1= max (buy1,- prices[i])
1016
+ sell1= max (sell1, buy1+ prices[i])
1017
+ buy2= max (buy2, sell1- prices[i])
1018
+ sell2= max (sell2, buy2+ prices[i])
1019
+ return sell2
1020
+ ```
1021
+
1022
+
1023
+ ###[ best-time-to-buy-and-sell-stock-iv] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/ )
1024
+
1025
+ 188 . 买卖股票的最佳时机 IV
1026
+ 给定一个整数数组 prices ,它的第 i 个元素 prices[ i] 是一支给定的股票在第 i 天的价格。
1027
+
1028
+ 设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
1029
+
1030
+ 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
1031
+
1032
+
943
1033
``` Python
1034
+ class Solution :
1035
+ def maxProfit (self ,k :int ,prices : List[int ]) ->int :
1036
+ days= len (prices)
1037
+ profit= 0
1038
+ if days< 2 :
1039
+ return profit
1040
+ if k>= days:
1041
+ for dayin range (1 , days):
1042
+ if prices[day]> prices[day- 1 ]:
1043
+ profit+= (prices[day]- prices[day- 1 ])
1044
+ return profit
1045
+ buy= [float (" -inf" )]* (k+ 1 )
1046
+ sell= [0 ]* (k+ 1 )
1047
+ for iin range (days):
1048
+ for jin range (1 , k+ 1 ):
1049
+ buy[j]= max (buy[j], sell[j- 1 ]- prices[i])
1050
+ sell[j]= max (sell[j], buy[j]+ prices[i])
1051
+ return sell[- 1 ]
1052
+ ```
1053
+
1054
+
1055
+ ###[ best-time-to-buy-and-sell-stock-with-transaction-fee] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ )
1056
+
1057
+ 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
1058
+
1059
+ 你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
1060
+
1061
+ 返回获得利润的最大值。
1062
+
1063
+ 注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
944
1064
1065
+
1066
+ ``` Python
1067
+ class Solution :
1068
+ def maxProfit (self ,prices : List[int ],fee :int ) ->int :
1069
+ n= len (prices)
1070
+ dp= [[0 ,- prices[0 ]]]+ [[0 ,0 ]for _in range (n- 1 )]
1071
+ for iin range (1 , n):
1072
+ dp[i][0 ]= max (dp[i- 1 ][0 ], dp[i- 1 ][1 ]+ prices[i]- fee)
1073
+ dp[i][1 ]= max (dp[i- 1 ][1 ], dp[i- 1 ][0 ]- prices[i])
1074
+ return dp[n- 1 ][0 ]
945
1075
```
946
1076
947
1077
1078
+ ###[ best-time-to-buy-and-sell-stock-with-transaction-fee] ( https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ )
1079
+
1080
+ 309 . 最佳买卖股票时机含冷冻期
1081
+ 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
1082
+
1083
+ 设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
1084
+
1085
+ 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
1086
+ 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
1087
+
1088
+
1089
+ ``` Python
1090
+ class Solution :
1091
+ def maxProfit (self ,prices : List[int ]) ->int :
1092
+ n= len (prices)
1093
+ if n< 2 :
1094
+ return 0
1095
+ buy= [0 ]* n
1096
+ sell= [0 ]* n
1097
+ sell_s= [0 ]* n
1098
+ buy[0 ]= - prices[0 ]
1099
+ for iin range (1 , n):
1100
+ buy[i]= max (buy[i- 1 ], sell[i- 1 ]- prices[i])
1101
+ sell_s[i]= buy[i- 1 ]+ prices[i]
1102
+ sell[i]= max (sell_s[i- 1 ], sell[i- 1 ])
1103
+ return max (sell[- 1 ], sell_s[- 1 ])
1104
+ ```
1105
+
948
1106
949
1107
##练习
950
1108