121 Best Time to Buy and Sell Stocks
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user