Skip to content

Instantly share code, notes, and snippets.

@Nirma
Created January 4, 2020 13:03
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 Nirma/5f5a947fbca7020da7a7fe5ad4fee077 to your computer and use it in GitHub Desktop.
Save Nirma/5f5a947fbca7020da7a7fe5ad4fee077 to your computer and use it in GitHub Desktop.
Top Down Rod Cutting DP Example in Swift (Inefficient Version)
func maxProfitRodCut(prices: [Int], length: Int) -> Int {
if length <= 0 {
return 0
}
var currentMax = Int.min
for index in (1...length) {
currentMax = max(currentMax, prices[index - 1] + maxProfitRodCut(prices: prices, length: length - index))
}
return currentMax
}
maxProfitRodCut(prices: [1,6,3,6], length: 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment