121.E 买卖股票的最佳时机
思路
Code
func maxProfit(prices []int) int {
if len(prices) == 0 {
return 0
}
profit := 0
lowest := prices[0]
for _, price := range prices {
profit = max(profit, price-lowest)
lowest = min(lowest, price)
}
return profit
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}Last updated