forked from VeldaKiara/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path121.besttimetobuyandsell.py
24 lines (23 loc) · 1004 Bytes
/
121.besttimetobuyandsell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# buy left, sell right
left, right = 0, 1
# default case
max_profit = 0
#iterate through prices while our right corner has not passed the
# the end of prices
#iterate the right side while our right has not passed the end of prices
while right < len(prices):
#check if profitable
if prices[ left] < prices [ right]:
profit = prices[right] - prices [ left]
#check for the max of the current max profit with profit, the max ofboth
#will be th new max profit, if not remains the same
max_profit = max(max_profit, profit)
#if not profitable
else:
#update values we want our left to be at the lowest value, that is
#all the way to the right
left = right
right += 1
return max_profit