剑指 Offer 63. 股票的最大利润
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
#这道题只能买入卖出一次,而不是多次买入卖出
if not prices:
return 0
n = len(prices)
profits = [0] * n
#买入
buy = prices[0]
for i in range(1,n):
#price假设是当前卖出的价格
price = prices[i]
if price - buy >0:
profits.append(price-buy)
else:
#如果卖出减去买入小于0,将卖出价格赋值给买入价格
buy = price
return max(profits) #最后取出利润最大的值就是最大利润