Stock Buy-Sell Maximize Profit

class Solution(object):
    def maxProfit(self, prices):
        :type prices: List[int]
        :rtype: int
        if len(prices) == 0 or len(prices) == 1:
            return 0
        minP = prices[0]
        result = 0
        for price in prices:
            # Maintain the maximum profit at each price
            result = max(result, price - minP)
            # Maintain the minimum price seen so far
            minP = min(minP, price)
        if result < 0:
            return 0
        return result

Written with StackEdit.

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.

%d bloggers like this: