Last active
December 16, 2015 06:19
-
-
Save zidarsk8/5390872 to your computer and use it in GitHub Desktop.
ugly code
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
val memo = HashMap[(Int, Int), (Int, Int)]((0, 0) -> (0, 0)) | |
def cost(from: Int, to: Int): (Int, Int) = { | |
if (memo.contains((from, to))) { | |
memo((from, to)) | |
} else if (to - from < 1) { | |
(to, 0) | |
} else if (to - from < 3) { | |
(Math.max(0, to - 1), Math.max(0, to - 1)) | |
} else { | |
val costList = (from to to).map(i => (Math.max(cost(from, i - 1)._2, cost(i + 1, to)._2) + i)) | |
val result = (costList.lastIndexOf(costList.min) + 1, costList.min) | |
memo += (((from, to), (result))) | |
result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment