Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Signed Target

Learn
Play
Prove
Problem context and objectives
Mission briefing

Dungeon strategy engineer · A robot must cross a tiled dungeon

Store enough solved tile states to plan the complete route: The Signed Target.

A greedy step can trap the robot; recomputing every route is too expensive.

How you win

  1. 1Recognize when Target sum knapsack matches the clues
  2. 2Keep this true after every move: counts[sum] equals the number of sign choices for the processed prefix that produce sum
  3. 3Reach the result within O(n times number of reachable sums)

Rules and pressure

  • Target cost: O(n times number of reachable sums)
  • State rule: counts[sum] equals the number of sign choices for the processed prefix that produce sum
Lesson 1 of 3

Live algorithm trace

Target sum knapsack

Complete execution
1 of 6
1
number
1
1
1
1

The empty prefix has one way to make zero.

1counts = {0: 1}
2for each number
3create an empty next map
4for each reachable sum and count
5add count to sum + number and sum - number
6return count at target
counts = 0:1target = 3
Truth to preserve / Cost target
Truth to preserve

counts[sum] equals the number of sign choices for the processed prefix that produce sum

Cost target

O(n times number of reachable sums)

After each number, every reachable sum branches into adding or subtracting that number. A map stores how many sign assignments reach each resulting sum.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead