LeetCode 121 – Python 3 (Week 2 – 08)

Solution 1

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        
        max_profit = 0
        min_price = float('inf') #there is no infinite integer.
        
        for price in prices:
            min_price = min(price, min_price)
            max_profit = max(price-min_price, max_profit)
            
        return max_profit

Time complexity is 0(n). Space complexity is O(1).

Leave a comment

Design a site like this with WordPress.com
Get started