121.go 405 B

1234567891011121314151617181920212223
  1. package main
  2. func maxProfit(prices []int) int {
  3. cost, profit := 1<<31-1, 0
  4. for _, v := range prices {
  5. if v < cost {
  6. cost = v
  7. }
  8. if v-cost > profit {
  9. profit = v - cost
  10. }
  11. }
  12. return profit
  13. }
  14. // func main() {
  15. // a1 := []int{1, 2, 3, 4, 5}
  16. // a2 := []int{5, 4, 3, 2, 1}
  17. // a3 := []int{8, 1}
  18. // fmt.Println(maxProfit(a1))
  19. // fmt.Println(maxProfit(a2))
  20. // fmt.Println(maxProfit(a3))
  21. // }