题解 | #买卖股票的最好时机(一)# | Golang
买卖股票的最好时机(一)
https://www.nowcoder.com/practice/351b87e53d0d44928f4de9b6217d36bb
package main import ( "fmt" ) type Solution struct { prices []int } func (t Solution) getMaxProfit() int { max_profit := 0 min_price := t.prices[0] for _,p := range t.prices { max_profit = max(max_profit, p - min_price) min_price = min(min_price, p) } return max_profit } func max(a int, b int) int { if a > b { return a } return b } func min(a int, b int) int { if a < b { return a } return b } func main() { n := 0 fmt.Scan(&n) prices := []int{} for n >=1 { p := 0 fmt.Scan(&p) prices = append(prices, p) n-- } fmt.Print(Solution{prices}.getMaxProfit()) }