Skip to content

Instantly share code, notes, and snippets.

@ecerney
Created April 22, 2016 04:51
Show Gist options
  • Save ecerney/07f7dc11e95efa5cf1d676ca7948c26e to your computer and use it in GitHub Desktop.
Save ecerney/07f7dc11e95efa5cf1d676ca7948c26e to your computer and use it in GitHub Desktop.
func maxProfit(prices: [Int]) -> Int {
if prices.count < 2 {
return 0
}
var profit = 0
var min = prices[0]
for price in prices {
if price - min > profit {
profit = price - min
}
if price < min {
min = price
}
}
return profit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment