-
121. 买卖股票的最佳时机 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
-
122. 买卖股票的最佳时机 II https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
-
123. 买卖股票的最佳时机 III https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/
-
188. 买卖股票的最佳时机 IV https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/
-
714. 买卖股票的最佳时机含手续费 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
121. 买卖股票的最佳时机
- 题意:给定一个数组,只能完成一笔交易,获取最大利润。
- 思路:找到每个最小值之后对应的最大值,减掉这个最小值得到的数中最大的即为所求。
- 做法:一直记录当前最小值,并不断更新当前值减最小值得到结果。
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:return 0
min_ = prices[0]
profit = 0
for i in range(1, len(prices)):
profit = max(profit, prices[i] - min_)
min_ = min(prices[i], min_)
return profit
122. 买卖股票的最佳时机 II
- 题意:可以进行无限次交易,获得最大利润。
- 思路:得到每个升序子数组。连续上升数组可以通过子数组相加,不变。如[1,2,3]利润为3-1=(2-1)+(3-2)
- 做法:每个数组值比前一个大就可以加到利润里。
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices) == 0:
return 0
profit = 0
last = prices[0]
for i in range(1, len(prices)):
if prices[i] > last:
profit += prices[i] - last
last = prices[i]
return profit
714. 买卖股票的最佳时机含手续费
- 题意:还是可以进行无限次交易,但是每笔交易需要手续费,获取最大利润。
- 思路:
- 和II的不同在于,每次交易需要手续费。
- 动态规划,用两个数组存储当前时刻的两种状态:持有hold/非持有(卖出)sold。
- hold[i]:当前有股票,要么是i-1就有,要么是i-1给卖出去(非持有)i时刻又买了。hold[i] = max(hold[i-1], sold[i - 1] - price[i]).
- sold[i]:当前没股票,要么是i-1就没有,要么是i-1有,i时刻给卖了。sold[i] = max(sold[i - 1], hold[i - 1] + price[i] - fee).
- 我们最后一刻一定是手里没有没卖出去的股票的,不然就亏了,所以只需要关注sold的最终值就是利润。
- 返回值是sold[-1]。
- 实际记录的时候只需要记录前面的值,所以不需要用数组来做,存储两个值即可。
代码:
class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
if len(prices) < 0:
return 0
sold, hold = 0, -prices[0]
# sold ___no stock : no stock at t - 1 or sell at t
# hold ___have stock : have stock at t - 1 or buy at t
for i in range(1, len(prices)):
sold = max(sold, hold + prices[i] - fee)
hold = max(hold, sold - prices[i])
return sold
188. 买卖股票的最佳时机 IV
- 题意:最多可以进行k次交易,获取最大利润。
- 思路:
- 和II的不同在于,不能交易那么多次。
- 那么当k>len/2时,等同于2。可以遍历整个数组得到最大利润。
- k<=len/2时,用动态规划的思想。用两个数组存储,和上一个带手续费的类似。
- 需要考虑每次交易的情况。买一定在卖之前。
- 这一次存储的是一个交易序列,sold[i]代表第i次交易后非持有的最大利润,hold[i]代表第i次交易后持有第最大利润。
- i表示数组的索引,j代表交易次数。如,sold[i][j]代表数组中第三个数在第j次交易后非持有的最大利润。
- 注意,求持有的时候,有种情况是上一次卖出sold这一次买入,j是不同的,应买在卖之后,所以是前一次卖,j-1交易。
代码:
class Solution(object):
def maxProfit(self, k, prices):
"""
:type k: int
:type prices: List[int]
:rtype: int
"""
if len(prices) < 2:
return 0
if k > len(prices)/ 2:
profit = 0
if len(prices) == 0:
return profit
for i in range(1, len(prices)):
if prices[i] - prices[i - 1] > 0:
profit += prices[i] - prices[i - 1]
return profit
sold, hold = [0], [-2147483648]
for i in range(len(prices)):
sold.append(0)
hold.append(-2147483648)
for i in range(len(prices)):
for j in range(1, k + 1):
sold[j] = max(sold[j], hold[j] + prices[i])
hold[j] = max(hold[j], sold[j - 1] - prices[i])
return sold[k]
123. 买卖股票的最佳时机 III
- 题意:和IV一样,只允许交易两次。
- 思路:其实是IV的特例,可以直接用IV的代码,设置k=2。但是有点大材小用。
- 交易序列有着明显的顺序,buy1,sell1,buy2,sell2。
- buy1[0] = buy2[0] = -prices[i], sell1[0]=sell2[0]=0。
- 这里索引0代表初始值,下同。实际上并不需要数组存储,只需要四个数值。
- buy1[i] = max(buy1[i - 1], -prices[i])
- sell1[i] = max(sell1[i - 1], buy1[i] + prices[i])
- buy2[i] = max(buy2[i - 1], sell1[i]-prices[i]),从第一次卖出后,买入算利润。
- sell2[i] = max(sell2[i], buy2[i] + prices[i]), 这里已经进行了买——卖——买,直接算第二次买入后卖出的利润即可。
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type k: int
:type prices: List[int]
:rtype: int
"""
if len(prices) < 2:
return 0
sell1, buy1, sell2, buy2 = 0, -prices[0], 0, -prices[0]
for i in range(1, len(prices)):
buy1 = max(buy1, -prices[i])
sell1 = max(sell1, buy1 + prices[i])
buy2 = max(buy2, sell1 - prices[i])
sell2 = max(sell2, buy2 + prices[i])
return max(0, sell2, sell1)