Best Time to Buy and Sell Stock II
Problem statement
Same daily prices array, but you may complete as many buy→sell cycles as you like. You cannot hold more than one share at a time. Return the maximum total profit.
Example:
Input:
[7, 1, 5, 3, 6, 4]
Expected output:
7
Why: take the uphill segments 1→5 and 3→6.
Practice on LeetCode: Best Time to Buy and Sell Stock II
Golang Solution
func maxProfit(prices []int) int {
profit := 0
for i := 1; i < len(prices); i++ {
if prices[i] > prices[i-1] {
profit += prices[i] - prices[i-1]
}
}
return profit
}
