Tap here to switch tabs
Problem
Ask
Submissions

Problem: 0/1 Knapsack

med
30 min
Explore the 0 1 Knapsack problem to understand how to select items maximizing total value without exceeding weight capacity. Learn dynamic programming techniques to build optimized solutions that handle constraints efficiently and practice implementing your solution step by step.

Statement

You are given nn items whose weights and values are known, as well as a knapsack to carry these items. The knapsack cannot carry more than a certain maximum weight, known as its capacity.

You need to maximize the total value of the items in your knapsack, while ensuring that the sum of the weights of the selected items does not exceed the capacity of the knapsack.

If there is no combination of weights whose sum is within the capacity constraint, return 00.

Notes:

  1. An item may not be broken up to fit into the knapsack, i.e., an item either goes into the knapsack in its entirety or not at all.
  2. We may not add an item more than once to the knapsack.

Constraints:

  • 11 \leq capacity 1000\leq 1000
  • 11 \leq values.length 500\leq 500
  • weights.length ==== values.length
  • 11 \leq values[i] 1000\leq 1000
  • 11 \leq weights[i] \leq capacity
Tap here to switch tabs
Problem
Ask
Submissions

Problem: 0/1 Knapsack

med
30 min
Explore the 0 1 Knapsack problem to understand how to select items maximizing total value without exceeding weight capacity. Learn dynamic programming techniques to build optimized solutions that handle constraints efficiently and practice implementing your solution step by step.

Statement

You are given nn items whose weights and values are known, as well as a knapsack to carry these items. The knapsack cannot carry more than a certain maximum weight, known as its capacity.

You need to maximize the total value of the items in your knapsack, while ensuring that the sum of the weights of the selected items does not exceed the capacity of the knapsack.

If there is no combination of weights whose sum is within the capacity constraint, return 00.

Notes:

  1. An item may not be broken up to fit into the knapsack, i.e., an item either goes into the knapsack in its entirety or not at all.
  2. We may not add an item more than once to the knapsack.

Constraints:

  • 11 \leq capacity 1000\leq 1000
  • 11 \leq values.length 500\leq 500
  • weights.length ==== values.length
  • 11 \leq values[i] 1000\leq 1000
  • 11 \leq weights[i] \leq capacity