← All questions
RareProgramming

What feature makes a problem a good candidate for dynamic programming?

Short answer
Subproblems overlap

Exercise

  1. Subproblems never repeat
  2. It asks for a shortest path
  3. Subproblems overlap
  4. It involves strings
  5. Exhaustive search is always faster

Solution

The same subproblem can be solved once and its result reused.

In a recursive calculation of F(5), F(3) is requested directly and inside F(4). What repeats is the same question with the same input. Saving F(3) lets both branches reuse its answer. The mere presence of strings or paths in a problem does not establish that any subproblem repeats.

F(5) = F(4) + F(3), F(4) = F(3) + F(2). F(0) = 0, F(1) = 1, F(2) = 1, F(3) = 2, F(4) = 3, F(5) = 5.

A state must uniquely specify the remaining subproblem. Different routes through the computation that reach the same state can reuse the same previously computed value.

Dynamic programming

Platforms

  • MAXIMUM

Download now and try it for free on your own tasks!

10 15 free credits • No credit card required
Version: Loading…

Choose the Windows app or the Chrome extension with phone controls.