Stocks Maximize Profit, Multiple Buy & Sell

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) < 2:
            return 0
        
        minP = prices[0]
        result = 0
        
        for price in prices:
            if minP == float('inf'):
                minP = price
                
            profit = (price - minP)
            if profit > 0:
                result += profit
                minP = float('inf')
            
            minP = min(price, minP)

            
        return result

Reference

Written with StackEdit.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.