solulubeta
Sign in
Recursion & Memo / DP patterns for coding tests
04.04 · Lesson

Take or Skip With a Target

The most common DP in tests: each item used at most once, and a target or capacity that shrinks when you take one. State (i, rem), two choices. Covers subset sum, knapsack and equal partition.

10 min 2 videos Quiz Pro

If you learn one DP pattern well, make it this one.

The shape

  • Each item can be used at most once.
  • There is a target: a sum, a budget or a capacity.
  • Taking an item uses up part of the target.
f(i, rem) = the answer using items i..n-1, when rem is what's left of the target
  • take item i (only if it fits) → f(i + 1, rem − w[i])
  • skip item i → f(i + 1, rem)
solulu Pro

Continue this lesson with Pro

Inside: 5 sections · 2 videos · quiz.

  • The shape
  • See it
  • One recursion, three questions
  • Questions that are subset sum in disguise
  • Derive it yourself
Lifetime ₹1,999 ₹999 · Launch offer
Mark this as done when you are finished.

Item 30 of 44 · updated 2026-10-13