63.股票的最大利润
Last updated
Last updated
func maxProfit(_ prices: [Int]) -> Int {
guard prices.isEmpty == false else {
return 0
}
var p = 0, res = 0, bottom = prices[0]
while p < prices.count {
let item = prices[p]
if item <= bottom {
bottom = item
}
else if item - bottom > res {
res = item - bottom
}
p += 1
}
return res
}