https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150
go
package leetcode150 import "testing" func TestMaxProfit2(t *testing.T) { prices := []int{7, 1, 5, 3, 6, 4} res := maxProfit2(prices) println(res) } func maxProfit2(prices []int) int { if len(prices) <= 1 { return 0 } i := 0 total := 0 for i+1 < len(prices) { for ; i < len(prices); i++ { if i+1 < len(prices) && prices[i] >= prices[i+1] { continue } break } minPrice := prices[i] for ; i < len(prices); i++ { if i+1 < len(prices) && prices[i] <= prices[i+1] { continue } break } maxPrice := prices[i] total += maxPrice - minPrice } return total }
标签:150,int,testing,len,II,122,prices,leetcode From: https://www.cnblogs.com/MoonBeautiful/p/18357604