Greedy Algorithms - Best Time to Buy and Sell Stock II
Examine the following code snippet for the peak-valley approach. Which line causes incorrect profit calculation?
def maxProfit(prices):
i = 0
profit = 0
while i < len(prices) - 1:
while i < len(prices) - 1 and prices[i] >= prices[i+1]:
i += 1
valley = prices[i]
while i < len(prices) - 1 and prices[i] <= prices[i+1]:
i += 1
peak = prices[i]
profit += peak - valley
return profit
