码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-10 17:03:20      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:nsa   rmi   div   英文   pre   design   sign   复杂度   class   

[抄题]:

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

标签:nsa   rmi   div   英文   pre   design   sign   复杂度   class   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!