题目:
题解:
- func maxProfit(prices []int) int {
- if len(prices) == 0 {
- return 0
- }
- n := len(prices)
- f0, f1, f2 := -prices[0], 0, 0
- for i := 1; i < n; i++ {
- newf0 := max(f0, f2 - prices[i])
- newf1 := f0 + prices[i]
- newf2 := max(f1, f2)
- f0, f1, f2 = newf0, newf1, newf2
- }
- return max(f1, f2)
- }
- func max(x, y int) int {
- if x > y {
- return x
- }
- return y
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |