leetcode121- Best Time to Buy and Sell Stock

难度: 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.


leetcode121- Best Time to Buy and Sell Stock

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

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

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

                                                           2:先买进再卖出。

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

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

leetcode121- Best Time to Buy and Sell Stock