leetcode121.买卖股票的最佳时机

func maxProfit(prices []int) int {
    dp:=make([][2]int,len(prices))
    for i,price:=range prices{
        if i==0{
            dp[i][0]=0
            dp[i][1]=-price
        }else{
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]+price)
            dp[i][1]=max(dp[i-1][1],-price)
        }
    }
    return dp[len(prices)-1][0]
}

Last updated