Best Time to Buy and Sell Stock LeetCode solution in Python

1
# Runtime: 2509 ms (Top 5.01%) | Memory: 25 MB (Top 86.17%)
2

3

4
class Solution:
5
def maxProfit(self, prices):
6
left = 0 # Buy
7
right = 1 # Sell
8
max_profit = 0
9
while right < len(prices):
10
currentProfit = prices[right] - prices[left] # our current Profit
11
if prices[left] < prices[right]:
12
max_profit = max(currentProfit, max_profit)
13
else:
14
left = right
15
right += 1
16
return max_profit

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0