From 059776458a36a49b327751093997ae833164a2b9 Mon Sep 17 00:00:00 2001 From: izatwin Date: Sat, 29 Jun 2024 10:18:18 -0400 Subject: [PATCH] 121 Best Time to Buy and Sell Stocks --- .../Solution.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 easy/121 Best Time to Buy and Sell Stock/Solution.py diff --git a/easy/121 Best Time to Buy and Sell Stock/Solution.py b/easy/121 Best Time to Buy and Sell Stock/Solution.py new file mode 100644 index 0000000..146ce19 --- /dev/null +++ b/easy/121 Best Time to Buy and Sell Stock/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + max_profit = 0 + buy_index = 0 + if len(prices) == 1: + return 0 + for i, cur_price in enumerate(prices): + profit = cur_price - prices[buy_index] + if profit > max_profit: + max_profit = profit + elif cur_price < prices[buy_index]: + buy_index = i + + return max_profit