DEV Community

Cover image for Dynamic Programming
Federico Diaz Aguirre
Federico Diaz Aguirre

Posted on

4 1 1 1 2

Dynamic Programming

Context

When facing recursive problems we quickly identify the memorization enhancement to avoid wasting cycles for past computations. But what about the underlying implementations top-down vs bottom-up?

In the video below I am comparing these two. And the drawbacks of using BFS in this particular case:

  1. Higher memory usage due to maintaining a queue and a visited set.
  2. Worse than DP for large amount values (since BFS explores level by level, it can be slow for high values).

Summary

Visual representation

Image description

Comparisson

Approach Time Complexity Space Complexity Notes
Recursive DP (Top-Down w/ Memoization) O(amount × len(coins)) O(amount) Efficient, but recursion uses stack memory
Iterative DP (Bottom-Up) O(amount × len(coins)) O(amount) Best for dense subproblem coverage
BFS (Graph Traversal) O(amount × len(coins)) (Worst case) O(amount) Can be more efficient for small values

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs