您的位置:首页 > 其它

leetcode121- Best Time to Buy and Sell Stock

2017-11-25 12:31 363 查看
难度: easy

Say you have an array for which the ith element is the price of a given stock on day i.

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.



题意:第一次碰到这种题目,原题交代的不清楚,花了很长时间才搞懂。

         list中每一个element代表当天的股票价值。

        本题要求:必须盈利的情况下,1:低价买进(数较小)高价卖出(数较大):

                                                           2:先买进再卖出。

        所以本题的意思是选出两个数字,一个较大,一个较小(不一定是list里面最小和最大的,数字所在的位置很重要),较小的数在较大的数前面,然后使满足条件的两个数的差(较大数减去较小数)最大。

思路:阅历全list的方法,找到较大数在较小数后面的最大差值。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode stock