Workspace Dynamic Programming
0d
DSA topics

Dynamic Programming

Define a state whose value can be reused. Derive a recurrence from the final choice, then evaluate states in dependency order.

Picture the idea

For a train pass itinerary, write the best cost up to each day in a notebook. The next decision can read those earlier costs directly.

When to use it

Different decision paths ask the same subquestion and each answer can be built from a bounded set of earlier answers.

How it works

For stair jumps of one or two, routes to step i come from i−1 or i−2. These last-jump groups are disjoint, so their counts add.

  • Name the state and its units.
  • Initialize reachable base states.
  • Evaluate the recurrence in order.
  • Keep predecessor choices if reconstructing a path.

Count reachable states and multiply by work per state. A rows-by-columns grid with two constant-time predecessors costs O(rows·columns) time.

Watch for this mistake

An initialized zero may incorrectly represent an unreachable state in a minimization problem.

Explain the state in a full sentence before writing the recurrence.

Practice in the lab

Related learning