Skip to content

Grind 75 - Best Time to Buy and Sell Stock

Posted on:August 4, 2023 at 12:29 AM at 1 min read

Solution for Merge Best Time to Buy and Sell Stock problem which is a part of Grind 75 list.

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        left = 0
        right = 1
        profit = 0

        while right < len(prices):
            curr_profit = prices[right] - prices[left]
            if prices[left] < prices[right]:
                profit = max(curr_profit, profit)
            else:
                left = right
            right += 1

        return profit

Comments