Best Time to Buy and Sell Stock @ LeetCode (Python)
Best Time to Buy and Sell Stock III @ LeetCode (Python)

Best Time to Buy and Sell Stock II @ LeetCode (Python)

kitt posted @ 2014年3月17日 00:54 in LeetCode , 1942 阅读

任何时候只能最多持有一股, 即每天的选择就是买入一股, 卖出一股或者不行动, 要卖出股票的话手头必须有一股。只要遇到递增序列, 就赚到了profit。

class Solution:
    # @param prices, a list of integer
    # @return an integer
    def maxProfit(self, prices):
        length = len(prices)
        profit = 0
        for i in xrange(1, length):
            if prices[i] > prices[i - 1]:
                profit += prices[i] - prices[i - 1]
        return profit

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter