题解 | #买卖股票的最好时机(一)# | 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())
}

全部评论

相关推荐

爱睡觉的冰箱哥:学历不够啊兄弟,分析师都是9硕✌🏻咱搞不过他们滴
点赞 评论 收藏
分享
一表renzha:你点进去没打招呼他也会有提示的,之前我点进美的,还没打招呼,他马上给我发了不太合适哦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务