Created
April 16, 2019 05:10
-
-
Save twhitt14/db1604db37c16f75588403b79397e691 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
// Run in an Xcode Playground | |
let prices: [Double] = [6,0,-1,10,11,12,1,5,200,-2,200] | |
func solution(prices: [Double]) -> Double { | |
var possibleOutcomes: [Double] = [] | |
prices.enumerated().forEach { tuple in | |
let index = tuple.offset | |
let initialPrice = tuple.element | |
let pricesAfterInvestment = prices.suffix(from: index) | |
pricesAfterInvestment.forEach { finalPrice in | |
possibleOutcomes.append(finalPrice - initialPrice) | |
} | |
} | |
return possibleOutcomes.max() ?? 0 | |
} | |
solution(prices: prices) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment