动态规划(5)

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if prices == []:
            return 0
        min = prices[0]
        max_num = 0
        for i in range(len(prices)):
            if prices[i] < min:
                min = prices[i]
            max_num = max(max_num, prices[i]-min )
        return max_num
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if prices == []:
            return 0
        min = prices[0]
        max_num = 0
        #空间节省了,当时时间却增多了
        for i in range(1,len(prices)):
            if prices[i] < min:
                min = prices[i]
            max_num = max(max_num, prices[i]-min )
        return max_num

猜你喜欢

转载自www.cnblogs.com/topass123/p/12635861.html
今日推荐