1234567891011121314151617181920212223 |
- package main
- func maxProfit(prices []int) int {
- cost, profit := 1<<31-1, 0
- for _, v := range prices {
- if v < cost {
- cost = v
- }
- if v-cost > profit {
- profit = v - cost
- }
- }
- return profit
- }
- // func main() {
- // a1 := []int{1, 2, 3, 4, 5}
- // a2 := []int{5, 4, 3, 2, 1}
- // a3 := []int{8, 1}
- // fmt.Println(maxProfit(a1))
- // fmt.Println(maxProfit(a2))
- // fmt.Println(maxProfit(a3))
- // }
|