Best Time to Buy and Sell Stock

Problem page:https://leetcode.com/problems/best-time-to-buy-and-sell-stock

Solution

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        buy = prices[0]
        profit = 0
        for i in range(1, len(prices)):
            if prices[i] < buy:
                buy = prices[i]
            elif prices[i] - buy > profit:
                profit = prices[i] - buy
        return profit

Complexity

  • time: O(n)
  • space: O(1)