-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path121.py
More file actions
21 lines (15 loc) · 689 Bytes
/
Copy path121.py
File metadata and controls
21 lines (15 loc) · 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#You are given an array prices where prices[i] is the price of a given stock on the ith day.
#You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
#Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
class Solution:
def maxProfit(self, prices: List[int]) -> int:
l, r = 0, 1
output = 0
while r < len(prices):
if prices[l] > prices[r]:
l = r
r += 1
else:
output = max(output, prices[r]-prices[l])
r += 1
return output