123456789101112131415161718192021222324252627 |
- package main
- import (
- "fmt"
- )
- 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{7, 4, 3, 8, 1}
- fmt.Println(maxProfit(a1))
- fmt.Println(maxProfit(a2))
- fmt.Println(maxProfit(a3))
- }
|