- LeetCode #121 #122 #123 #188 #30
- #121. Best Time to Buy and Sell
- 【2019-07-30】leetcode(121-130)
- 【leetcode】121、Best Time to Buy a
- LeetCode #121: Best Time to Buy
- Leetcode 121 Best time to buy an
- leetcode:121. Best Time to Buy a
- [LeetCode] 121. Best Time to Buy
- Leetcode121 - 123 (dp problems)
- [数组]121. Best Time to Buy and Se
翻译:假设有一个数组,它的第i项是第i天的股票价格。如果你最多只能进行一次买卖操作(买一次,卖一次),设计一个算法求出最大的收益。
即求max(array[j]-array[i]), j>=i
思路:遍历的过程中,用一个变量保存目前为止最小的数,用当前的数与目前最小的数相减,判断这个差与此前得到的最大收益的大小,取较大值更新最大收益
price = [3,2,8,3,2,7,5,13]
def maxpro(price):
minv= price[0]
maxpro =0
for i in price:
minv =min(i,minv)
maxpro =max(i-minv,maxpro)
print maxpro
maxpro(price)









网友评论