Quotes help make search much faster. Example: "Practice Makes Perfect"

Friday, December 9, 2016

Codefights knapsackLight


//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


This is an example of scrolling text using Javascript.

Popular Posts