Best Time to Buy and Sell Stock II LeetCode solution in Python

1
class Solution:
2
def maxProfit(self, prices: List[int]) -> int:
3
n = len(prices)
4
ans = 0
5
want = "valley"
6
for i in range(n - 1):
7
if want == "valley" and prices[i] < prices[i + 1]:
8
ans -= prices[i]
9
want = "hill"
10
elif want == "hill" and prices[i] > prices[i + 1]:
11
ans += prices[i]
12
want = "valley"
13
if want == "hill":
14
ans += prices[-1]
15
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0