Created
April 22, 2016 04:51
-
-
Save ecerney/07f7dc11e95efa5cf1d676ca7948c26e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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