Skip to content

Instantly share code, notes, and snippets.

@Yunfly
Created May 29, 2020 03:13
Show Gist options
  • Save Yunfly/5fd2bbda99f7bbee0888f65134918c48 to your computer and use it in GitHub Desktop.
Save Yunfly/5fd2bbda99f7bbee0888f65134918c48 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zaqatop
<!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>
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