From 3ac2ea0462a673d261e0094f96d4560e2c49e3c6 Mon Sep 17 00:00:00 2001 From: dolphinflow86 Date: Mon, 20 Jul 2026 22:01:36 +0900 Subject: [PATCH] best time to buy and sell stock solution --- best-time-to-buy-and-sell-stock/dolphinflow86.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/dolphinflow86.py diff --git a/best-time-to-buy-and-sell-stock/dolphinflow86.py b/best-time-to-buy-and-sell-stock/dolphinflow86.py new file mode 100644 index 0000000000..c2df5e394d --- /dev/null +++ b/best-time-to-buy-and-sell-stock/dolphinflow86.py @@ -0,0 +1,13 @@ +# 1) Keep track of local minimum and use local minimum to update max profile while interating prices. +# TC: O(N) where N is the length of prices +# SC: O(1) +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = prices[0] + max_profit = 0 + + for price in prices: + max_profit = max(max_profit, price - min_price) + min_price = min(min_price, price) + + return max_profit