Skip to content

Instantly share code, notes, and snippets.

@drewlesueur
Created November 23, 2010 05:12
Show Gist options
  • Save drewlesueur/711294 to your computer and use it in GitHub Desktop.
Save drewlesueur/711294 to your computer and use it in GitHub Desktop.
Algorithms homework :(
mcm = (p) ->
console.log "test"
m = []
s = []
n = p.length - 1
$(document.body).css "height", "20000px"
table = ["<table id='mcm' cellpadding=0 cellspacing=0 style='border-collapse: collapse; -webkit-transform: rotate(135deg) translate(-100px, -300px); '>"]
for a in [1..n]
table.push "<tr colspan='a'>"
for b in [1..a]
table.push "<td id='mcm_#{n-a+1}_#{n-b+1}' style='border:1px solid black; width: 50px; height: 50px;'>
<div style='-webkit-transform: rotate(-135deg);'>0</div>
</td>"
table.push "</tr>"
table.push "</table>"
$('#mcm').remove()
$(document.body).append table.join "\n"
for i in [1..n]
m[i] = []
m[i][i] = 0
s[i] = []
for L in [2..n]
for i in [1..n-L+1]
j = i+L-1
m[i][j] = Infinity
$("#mcm_#{i}_#{j}").find("div").text "infinity"
for k in [i..j-1]
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
if q < m[i][j]
m[i][j] = q
$("#mcm_#{i}_#{j}").find("div").text q
s[i][j] = k
console.log "s[#{i}][#{j}] = #{k}"
else
#console.log q, m[i][j]
#console.log "nothing"
window.s = s
#mcm [45, 22, 30, 20, 15]
#mcm [50, 20, 40, 30, 10]
mcm [30, 35, 15, 5, 10, 20, 25]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment