122. Best Time to Buy and Sell Stock II
Medium
Problem:
Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.Solution:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# result = 0
# for i in range(len(prices) - 1):
# if prices[i + 1] > prices[i]:
# result += prices[i + 1] - prices[i]
# return result
return sum(max(prices[i + 1] - prices[i], 0) for i in range(len(prices) - 1))Last updated