From 43065b3e529ba3c54ea7c5a82f0a15cd2b957f7a Mon Sep 17 00:00:00 2001 From: AfroLogicInsect <51209193+AkanimohOD19A@users.noreply.github.com> Date: Thu, 1 May 2025 14:40:07 +0100 Subject: [PATCH] Update Best Time to Buy and Sell Stock - Leetcode 121.py accounts for the irreversible day.. --- ... Time to Buy and Sell Stock - Leetcode 121.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py b/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py index faf4100..c6e4240 100644 --- a/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py +++ b/Best Time to Buy and Sell Stock - Leetcode 121/Best Time to Buy and Sell Stock - Leetcode 121.py @@ -48,3 +48,19 @@ def maxProfit(self, prices: List[int]) -> int: max_profit = max(profit, max_profit) return max_profit + +# Takes account of 1st day +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = prices[0] # Day1 and we can't go back in time + max_profit = 0 + + for price in prices: + if price < min_price: #e.g 7 =7, 7 >1 + min_price = price + else: + current_profit = price - min_price # then: cp = 7 - 7:0; 7 - 1: 6 + if current_profit > max_profit: # 6>0 + max_profit = current_profit + + return max_profit # 6