Best Time to Buy and Sell Stock II LeetCode solution in Python
1class Solution:2def maxProfit(self, prices: List[int]) -> int:3n = len(prices)4ans = 05want = "valley"6for i in range(n - 1):7if want == "valley" and prices[i] < prices[i + 1]:8ans -= prices[i]9want = "hill"10elif want == "hill" and prices[i] > prices[i + 1]:11ans += prices[i]12want = "valley"13if want == "hill":14ans += prices[-1]15return ans