题解 | #买卖股票的最好时机#
买卖股票的最好时机
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
维护一个min_,表示是i-1天的最低价格。
# # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here n = len(prices) if n < 2: return 0 min_ = prices[0] res = 0 for i in range(1, n): min_ = min(min_, prices[i-1]) res = max(res,prices[i] - min_) return res