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


Written with StackEdit.


Knowledge is the seed of wisdom.

Leave a Reply

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

You are commenting using your 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.