src/com/michaelho/DynamicProgramming/DP2.java
/**
     * The dynamic programming used to calculate knapsack problems with unlimited supply.
     * The runtime is O(NW) where N is the number of objects and W is the weight limit
     * of the knapsack. The runtime is the same as previous version of knapsack problem.
     *
     * @param wt The array represents the weights of input objects.
     * @param val The array represents the values of input objects.
     * @param w The total weight allowed remaining.
     * @return int The results of calculation.
     * */
The dynamic programming used to calculate knapsack problems with unlimited supply.
The runtime is O(NW) where N is the number of objects and W is the weight limit
of the knapsack. The runtime is the same as previous version of knapsack problem.

@param wt The array represents the weights of input objects.
@param val The array represents the values of input objects.
@param w The total weight allowed remaining.
@return int The results of calculation.
The dynamic programming used to calculate knapsack problems with unlimited supply . The runtime is O ( NW ) where N is the number of objects and W is the weight limit of the knapsack . The runtime is the same as previous version of knapsack problem . @param wt The array represents the weights of input objects . @param val The array represents the values of input objects . @param w The total weight allowed remaining . @return int The results of calculation .
int knapSackSol2(int[] wt, int[] val, int w) {
        if (w == 0) {
            return 0;
        }
        int[] maxVals = new int[w+1];
        for (int b = 0; b <= w; b++) {
            for (int i = 0; i < val.length; i++) {
                if (wt[i] <= b) {
                    maxVals[b] = Math.max(maxVals[b], maxVals[b-wt[i]] + val[i]);
                }
            }
        }
        return maxVals[w];
    }
int knapSackSol2 ( int [ ] wt , int [ ] val , int w ) { if ( w == 0 ) { return 0 ; } int [ ] maxVals = new int [ w + 1 ] ; for ( int b = 0 ; b <= w ; b ++ ) { for ( int i = 0 ; i < val . length ; i ++ ) { if ( wt [ i ] <= b ) { maxVals [ b ] = Math . max ( maxVals [ b ] , maxVals [ b - wt [ i ] ] + val [ i ] ) ; } } } return maxVals [ w ] ; }
The dynamic programming used to calculate knapsack problems with unlimited supply.
The dynamic programming used to calculate knapsack problems with unlimited supply .