func maxProfit(prices []int) int { // v-. // >s0_|rest // / \ // rest/ \ buy // / sell v // s2<-----s1<. // |_|rest // n := len(prices) if n <= 1 { return 0 } s0, s1, s2 := 0, -prices[0], -1<<32 for i := 1; i < n; i++ { s0, s1, s2 = maxInt(s0, s2), maxInt(s0-prices[i], s1), s1+prices[i] } return maxInt(s0, s2) } func maxInt(x, y int) int { if x < y { return y } return x }