0/1 Knapsack. — Cracked Java
// Data Structures & Algorithms · Dynamic Programming
SeniorCoding

0/1 Knapsack.

Problem. Given n items with weights wt[] and values val[], and a knapsack of capacity W, maximize the total value of items packed. Each item may be taken at most once (0/1).

Brute force — O(2ⁿ)

Every item is in or out — enumerate all subsets, keep the best feasible one. Exponential; fine only for tiny n.

2D DP — O(n·W) time and space

State dp[i][w] = best value using the first i items with capacity w. For each item, either skip it or take it (if it fits):

dp[i][w] = max(
    dp[i-1][w],                          // skip item i
    dp[i-1][w - wt[i]] + val[i])         // take it (only if wt[i] <= w)
int knapsack(int[] wt, int[] val, int W) {
    int n = wt.length;
    int[][] dp = new int[n + 1][W + 1];
    for (int i = 1; i <= n; i++)
        for (int w = 0; w <= W; w++) {
            dp[i][w] = dp[i - 1][w];                       // skip
            if (wt[i - 1] <= w)
                dp[i][w] = Math.max(dp[i][w],
                    dp[i - 1][w - wt[i - 1]] + val[i - 1]); // take
        }
    return dp[n][W];
}

1D rolling — O(n·W) time, O(W) space

Each row depends only on the row above, so collapse to one array. The key is iterating capacity right-to-left: when you read dp[w - wt], it must still hold the previous item's value (not yet overwritten this round), which is what enforces "each item once".

int knapsack(int[] wt, int[] val, int W) {
    int[] dp = new int[W + 1];
    for (int i = 0; i < wt.length; i++)
        for (int w = W; w >= wt[i]; w--)        // RIGHT-TO-LEFT
            dp[w] = Math.max(dp[w], dp[w - wt[i]] + val[i]);
    return dp[W];
}

Mark your status