Simple python algorithm


  • 0
    A
    class Solution:
        # @param {integer[]} prices
        # @return {integer}
        def maxProfit(self, prices):
            if prices is None:
                return 0
            
            p_len = len(prices)
            if p_len <=1:
                return 0
                
            p_min = prices[0]
            pr_max = 0
            
            for i in prices:
                if i >= p_min:
                    pr = i - p_min
                    if pr > pr_max:
                        pr_max = pr
                else:
                    p_min = i
                    
            return pr_max

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.