121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if len(prices) == 0: return 0 #最小值股价初始化为 极大值 min_price = float('inf') #最大利润初始化为 0 max_profit = 0 for price in prices: min_price = min(min_price,price) profit = price-min_price max_profit = max(max_profit,profit) return max_profit
以上