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