Skip to content

Instantly share code, notes, and snippets.

@zidarsk8
Last active December 16, 2015 06:19
Show Gist options
  • Save zidarsk8/5390872 to your computer and use it in GitHub Desktop.
Save zidarsk8/5390872 to your computer and use it in GitHub Desktop.
ugly code
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