DEV Community

Cover image for Dynamic Programming
Federico Diaz Aguirre
Federico Diaz Aguirre

Posted on

3 1 1 1

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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

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

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay