Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # same tc and sc for both - however -
- class Solution:
- def maxProfit(self, prices: List[int]) -> int:
- max_profit = 0
- recent_valley = prices[0]
- recent_peak = prices[0]
- i = 0
- while i < len(prices)-1:
- #find valley first
- while i < len(prices)-1 and prices[i+1]<=prices[i]:
- i += 1
- recent_valley = prices[i]
- # find peak then
- while i < len(prices)-1 and prices[i+1]>=prices[i]:
- i += 1
- recent_peak = prices[i]
- # calculate profit
- max_profit += recent_peak - recent_valley
- return max_profit
- class Solution:
- def maxProfit(self, prices: List[int]) -> int:
- max_profit = 0
- for i in range(1, len(prices)):
- if prices[i] > prices[i-1]:
- max_profit += prices[i] - prices[i-1]
- return max_profit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement