042_买卖股票的最佳时机
知识点:动态规划
LeetCode第一百二十一题:
看着不像DP,但还是有点轻微的DP味道在里面
语言:GoLang
func maxProfit(prices []int) int {
length := len(prices)
if length < 2 {
return 0
}
minPrice, maxProfit := math.MaxInt64, 0
for i := 0; i < length; i++ {
if prices[i] < minPrice {
minPrice = prices[i]
}
if prices[i] - minPrice > maxProfit {
maxProfit = prices[i] - minPrice
}
}
return maxProfit
}

浙公网安备 33010602011771号