Skip to interview prep
Apexed

79. 0/1 Knapsack

MediumDynamic programming

Given item weights, item values and a bag capacity, return the maximum total value you can carry. Each item can be taken at most once.

Example 1

Input: weights = [1, 3, 4, 5], values = [1, 4, 5, 7], capacity = 7
Output: 9
Explanation: items of weight 3 and 4

Constraints

  • 1 ≤ items ≤ 100
  • 0 ≤ capacity ≤ 1000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

0/1 Knapsack — coding interview practice — Apexed