//sample solution
function knapsackLight(value1, weight1, value2, weight2, maxW) {
// maxW >= weight1 + weight2
if((weight1+weight2)>maxW && (maxW >= weight1 || maxW >= weight2)){
if(weight1 > maxW){
return value2;
} else if(weight2 > maxW){
return value1;
} else if(value1 > value2){
return value1;
} else if(value2 > value1) {
return value2;
} else {
// the values are equal, bring heavier weight?
if(weight1 > weight2){
return value1;
} else if(weight2 > weight1){
return value2;
} else {
//weights are equal
return value1;
}
}
} else if(maxW < weight1 || maxW < weight2){
return 0;
}
else {
return (value1 + value2);
}
}
No comments:
Post a Comment