src/com/michaelho/DynamicProgramming/DP2.java
/**
     * The dynamic programming used to calculate knapsack problems. The runtime is
     * O(NW) where N is the number of objects and W is the weight limit of the knapsack.
     * Note that the runtime is not polynomial in the input size.
     * Reference: https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
     *
     * @param wt The array represents the values of input objects.
     * @param val The array represents the weights of input objects.
     * @param idx The index of the object.
     * @param w The total weight allowed remaining.
     * @return int The results of calculation.
     * */
The dynamic programming used to calculate knapsack problems. The runtime is
O(NW) where N is the number of objects and W is the weight limit of the knapsack.
Note that the runtime is not polynomial in the input size.

@param wt The array represents the values of input objects.
@param val The array represents the weights of input objects.
@param idx The index of the object.
@param w The total weight allowed remaining.
@return int The results of calculation.
The dynamic programming used to calculate knapsack problems . The runtime is O ( NW ) where N is the number of objects and W is the weight limit of the knapsack . Note that the runtime is not polynomial in the input size . @param wt The array represents the values of input objects . @param val The array represents the weights of input objects . @param idx The index of the object . @param w The total weight allowed remaining . @return int The results of calculation .
int knapSackSol1(int[] wt, int[] val, int idx, int w) {
        if (w == 0 || idx == wt.length) {
            return 0;
        }
        if (wt[idx] <= w)
            return Math.max(val[idx] + knapSackSol1(wt, val, idx+1, w-wt[idx]),
                    knapSackSol1(wt, val, idx+1, w));
        else
            return knapSackSol1(wt, val, idx+1, w);
    }
int knapSackSol1 ( int [ ] wt , int [ ] val , int idx , int w ) { if ( w == 0 || idx == wt . length ) { return 0 ; } if ( wt [ idx ] <= w ) return Math . max ( val [ idx ] + knapSackSol1 ( wt , val , idx + 1 , w - wt [ idx ] ) , knapSackSol1 ( wt , val , idx + 1 , w ) ) ; else return knapSackSol1 ( wt , val , idx + 1 , w ) ; }
The dynamic programming used to calculate knapsack problems.
The dynamic programming used to calculate knapsack problems .
// Base case