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

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

kitt posted @ 2014年3月16日 12:40 in LeetCode , 2857 阅读

扫描一遍,不断更新最大利润和最低价格即可。最大利润是当前价格减去最低价格。

class Solution:
    # @param prices, a list of integer
    # @return an integer
    def maxProfit(self, prices):
        if not prices: return 0 # prices is empty
        maxProfit = 0
        minPrice = prices[0]
        for currPrice in prices:
            minPrice = min(minPrice, currPrice)
            maxProfit = max(maxProfit, currPrice - minPrice)
        return maxProfit
Avatar_small
dewwen 说:
2014年7月14日 15:45

your answer is simple and clear. Thanks!
Working on leetcode problems these days, your blog really help me a lot.
:)


登录 *


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