Skip to content

Resource · Guide or article

Knapsack Problem

Formal definitions of the knapsack problem and its main variants, useful background before dynamic programming questions.

Reviewed

Open the original: Knapsack Problem (opens the original site in a new tab)

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. Example of a one-dimensional (constraint) knapsack problem: which boxes should be chosen to maximize the amount of money while still keeping the overall weight under or equal to 15 kg?

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • The optimization goal and a 15 kg box example.
  • 0/1 knapsack problem: each item taken zero or one time.
  • Bounded knapsack problem: at most c copies of each item.
  • Unbounded knapsack problem: any non-negative number of copies.

How to use it

PrepKit study advice, not a claim made by the source.

Build a 0/1 knapsack DP table by hand for four items, then code it and explain how the unbounded version changes the loop.

Scope and limitations

Definitions only: it does not explain a solution method or complexity. Formulas are images, and it relies on Wikipedia and a YouTube video.

Access and format

English README on GitHub, free, in the MIT-licensed trekhleb/javascript-algorithms project.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/sets/knapsack-problem/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email