首页 > 其他 > 详细

121. Best Time to Buy and Sell Stock买卖股票12

时间:2017-12-10 16:31:42      阅读:250      评论:0      收藏:0      [点我收藏+]

[抄题]:

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

[一句话思路]:贪心算法。不要用数组i,j角标,直接用min, profit表示价格,更方便。

[画图]:

[一刷]:

min, profit都记得要初始化

[总结]:

[复杂度]:

n/1

[英文数据结构]:

[其他解法]:

[题目变变变]:

maximum subarray最大求和子数组

 

121. Best Time to Buy and Sell Stock买卖股票12

原文:http://www.cnblogs.com/immiao0319/p/8017175.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!