Created
May 29, 2020 03:13
-
-
Save Yunfly/5fd2bbda99f7bbee0888f65134918c48 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zaqatop
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function main (bagV){ | |
const v = [0,3,4,5,6] | |
const w = [0,2,3,4,5] | |
let dp = new Array(5) | |
for (let i=0;i<5;i++){ | |
let array = new Array(bagV+1) | |
array.fill(0) | |
dp[i] = array | |
} | |
for (let i=1;i<5;i++){ | |
for (let j=1;j<=bagV;j++){ | |
if(j<w[i]){ | |
dp[i][j] = dp[i-1][j] | |
}else{ | |
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]) | |
} | |
} | |
} | |
let max = 0 | |
for(let i=0;i<5;i++){ | |
for(let j=1; j<=bagV;j++){ | |
max = Math.max(max,dp[i][j]) | |
} | |
} | |
return {dp,max} | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function main (bagV){ | |
const v = [0,3,4,5,6] | |
const w = [0,2,3,4,5] | |
let dp = new Array(5) | |
for (let i=0;i<5;i++){ | |
let array = new Array(bagV+1) | |
array.fill(0) | |
dp[i] = array | |
} | |
for (let i=1;i<5;i++){ | |
for (let j=1;j<=bagV;j++){ | |
if(j<w[i]){ | |
dp[i][j] = dp[i-1][j] | |
}else{ | |
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]) | |
} | |
} | |
} | |
let max = 0 | |
for(let i=0;i<5;i++){ | |
for(let j=1; j<=bagV;j++){ | |
max = Math.max(max,dp[i][j]) | |
} | |
} | |
return {dp,max} | |
}</script></body> | |
</html> |
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
function main (bagV){ | |
const v = [0,3,4,5,6] | |
const w = [0,2,3,4,5] | |
let dp = new Array(5) | |
for (let i=0;i<5;i++){ | |
let array = new Array(bagV+1) | |
array.fill(0) | |
dp[i] = array | |
} | |
for (let i=1;i<5;i++){ | |
for (let j=1;j<=bagV;j++){ | |
if(j<w[i]){ | |
dp[i][j] = dp[i-1][j] | |
}else{ | |
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]) | |
} | |
} | |
} | |
let max = 0 | |
for(let i=0;i<5;i++){ | |
for(let j=1; j<=bagV;j++){ | |
max = Math.max(max,dp[i][j]) | |
} | |
} | |
return {dp,max} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment