Skip to content

Instantly share code, notes, and snippets.

@ScottBuchanan
Last active August 29, 2015 14:19
Show Gist options
  • Save ScottBuchanan/5bcc2e132dd157ccbcba to your computer and use it in GitHub Desktop.
Save ScottBuchanan/5bcc2e132dd157ccbcba to your computer and use it in GitHub Desktop.
4.6
DP(i,j):
soln = i*n array with val inited to -1
return helper(i,j, soln)
helper(i, j, soln):
#first iterate through all the smallest lens, then build on those
for 1 to (j-i) as len:
#make j always len away from i
j = i + len - 1
bestsofar = infin
for i to j as k:
value = max(soln_check(soln, i, k-1), soln_check(soln, k+1, j)) + cost(k)
if value > bestsofar:
bestsofar = value
return soln[i][j]
soln_check(soln, i, j):
if j < i:
return 0
if i == j:
return cost(j)
else:
return soln[i][j]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment