# 0/1 Knapsack

Try to solve the 0/1 Knapsack problem.

## We'll cover the following

## Statement

You are given $n$ 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 $0$.

Notes:

- 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.
- We may not add an item more than once to the knapsack.

**Constraints:**

- $1 \leq$
`capacity`

$\leq 1000$ - $1 \leq$
`values.length`

$\leq 500$ `weights.length`

$==$`values.length`

- $1 \leq$
`values[i]`

$\leq 1000$ - $1 \leq$
`weights[i]`

$\leq$`capacity`

## Examples

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy