Skip to content

Instantly share code, notes, and snippets.

@twhitt14
Created April 16, 2019 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twhitt14/db1604db37c16f75588403b79397e691 to your computer and use it in GitHub Desktop.
Save twhitt14/db1604db37c16f75588403b79397e691 to your computer and use it in GitHub Desktop.
// 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