赞
踩
- func maxProfit(prices []int) int {
- n:=len(prices)
- if n==1 {
- return 0
- }
- buys:=make([]int,n)
- sells:=make([]int,n)
- buys[0],buys[1]=-prices[0],max(-prices[0],-prices[1])
- sells[0],sells[1]=0,max(sells[0],-prices[0]+prices[1])
- ans:=sells[1]
- for i:=2;i<n;i++ {
- buys[i]=max(buys[i-1],sells[i-2]-prices[i])
- sells[i]=max(sells[i-1],buys[i-1]+prices[i])
- ans=max(ans,sells[i])
- }
- return ans
- }
-
- func max(a,b int) int {
- if a>b {
- return a
- }
- return b
- }
- /*
- func maxProfit(prices []int) int {
- n:=len(prices)
- if n==1 {
- return 0
- }
- buys:=make([]int,n)
- sells:=make([]int,n)
- buys[0],buys[1]=-prices[0],-prices[1]
- sells[0],sells[1]=0,-prices[0]+prices[1]
- ans:=max(sells[0],sells[1])
- for i:=2;i<n;i++ {
- a,b:=0,buys[i-1]
- for j:=i-2;j>=0;j-- {
- a=max(a,sells[j])
- b=max(b,buys[j])
- }
- buys[i]=max(buys[i-1],a-prices[i])
- sells[i]=max(sells[i-1],b+prices[i])
- ans=max(ans,sells[i])
- }
- return ans
- }
- func max(a,b int) int {
- if a>b {
- return a
- }
- return b
- }
- */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。