解题思路: 只允许交易一次,顺序遍历一次,寻找与最小买卖价格差异最大的price[i]。
python3代码如下:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
minPrice = int(1e9)
maxProfit =0
for price in prices:
if price < minPrice:
minPrice = price
elif price-minPrice > maxProfit:
maxProfit = price-minPrice
return maxProfit
网友评论