class Solution:
"""
@param prices: Given an integer array
@return: Maximum profit
"""
def maxProfit(self, prices):
# write your code here
if(len(prices)==0):
return 0
lowest_price = prices[0]
ans = 0
for i in range(len(prices)):
ans = max(ans, prices[i]-lowest_price)
lowest_price = min(prices[i], lowest_price)
return ans
Lintcode:买卖股票的最佳时机
最新推荐文章于 2020-12-18 15:56:05 发布
本文介绍了一种用于确定股票买卖最佳时机以实现最大利润的算法。该算法通过一次遍历价格数组来找到最低买入价并计算可能的最大收益。


320

被折叠的 条评论
为什么被折叠?



