leetcode 股票问题
参考:labuladong
(leetcode-121) 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。
class Solution: def maxProfit(self, prices: List[int]) -> int: minp = float('inf') maxpr = float('-inf') if not prices: return 0 for i,v in enumerate(prices): minp = min(minp,v) maxpr = max(v-minp,maxpr) return maxpr
class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 dp = [[0,0] for _ in range(1+len(prices))] dp[0][1]=-prices[0] for i in range(1,len(prices)): dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i]) dp[i][1]=max(dp[i-1][1],-prices[i]) return dp[len(prices)-1][0]
(leetcode-122) 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
思路一:只要第二天价格高,我就买
class Solution: def maxProfit(self, prices: List[int]) -> int: sumr = 0 if len(prices) == 0: return 0 for i in range(1,len(prices)): tmp = prices[i] - prices[i-1] if tmp > 0: sumr += tmp return sumr
思路二:两种情况,cash表示我有现金:1)持有原来的;2)把股票卖出去,赚了当前股票的钱;hold表示持有股票:1)持有原来的股票;2)用cash买入股票;
class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 cash = 0 hold = -prices[0] n = len(prices) for i in range(1,n): cash = max(cash,hold+prices[i]) hold = max(hold,cash-prices[i]) return cash
(leetcode-714) 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。返回获得利润的最大值。
同leetcode 221 第二种思路一样,多了要付手续费;
class Solution: def maxProfit(self, prices: List[int], fee: int) -> int: ca = 0 hd = -prices[0] for i in range(1,len(prices)): ca = max(ca,hd+prices[i]-fee) hd = max(hd,ca-prices[i]) return ca
(leetcode-309) 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
思路:这时候买股票hold的时候的cash不是i-1的,而是i-2的
class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 cash = 0 hold = -prices[0] cash_pre = 0 for i in range(1,len(prices)): tmp = cash cash = max(cash,hold+prices[i]) hold = max(hold,cash_pre-prices[i]) cash_pre = tmp return cash
(leetcode-123)给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 cash_1 = 0 cash_2 = 0 hold_1 = -prices[0] hold_2 = -prices[0] for i in range(1,len(prices)): cash_1 = max(cash_1,hold_1+prices[i]) // 把第二次的股票卖了 hold_1 = max(hold_1,cash_2-prices[i]) // 拿第一次的钱买第二次的股票 cash_2 = max(cash_2,hold_2+prices[i]) // 把第一次持有的股票卖了 hold_2 = max(hold_2,-prices[i]) // 第一次买股票 return cash_1
(leetcode-188) 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: if not prices: return 0 n = len(prices) if k > n: sumr = 0 if len(prices) == 0: return 0 for i in range(1,len(prices)): tmp = prices[i] - prices[i-1] if tmp > 0: sumr += tmp return sumr g = [0] * (k + 1) l = [0] * (k + 1) for i in range(n - 1): diff = prices[i + 1] - prices[i] for j in range(k, 0, -1): l[j] = max(g[j - 1] + max(diff, 0), l[j] + diff) g[j] = max(l[j], g[j]) return g[-1]
来源:https://www.cnblogs.com/curtisxiao/p/11318648.html