leetcode:买卖股票的最佳时机

题目描述

解题思路

遍历数组寻找当前最小值保存,并计算当前所能获得的最大利润,若比先前大就更新最大利润

代码

1
2
3
4
5
6
7
8
9
10
11
class :
def maxProfit(self, prices: List[int]) -> int:
profit = 0
if len(prices)==0:
return 0
low=prices[0]
for i in range(len(prices)):
low = min(low, prices[i]);
profit = max(profit, prices[i] - low);
return profit