Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course

The "Memoization with Recursion" Lesson is part of the full, A Practical Guide to Algorithms with JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Binca reviews memoization and recursive approach to the "make change" problem.


Transcript from the "Memoization with Recursion" Lesson

>> Bianca Gandolfo: So how does our makeChange brute force solution change? [LAUGH] How does it change, how does it make change? The only difference really is that you're just caching it, right? So this here, this should also be a red flag, we talked about this, this is global cache, we don't really want that.

But to focus on the makeChange algorithm specifically, and not wasting a lot of time talking about memoization techniques that we mentioned earlier on how to make this better. The only change here is that we're gonna see, if it's already cached, we return it. So what this does is, as you're making these recursive calls, you're gonna short-circuit it each time.

And then, what else, yeah, that's pretty much it, yeah, you just need to make sure that you cache it every time. So there's two main differences there. And then of course, you can more responsibly memoize this as well by just passing it to your memoize function. Or even just adding your cache inside of your makeChange and returning a function like we did before.

And passing in the coins, so that you're not relying on this exterior data. You don't wanna hard-code it, you wanna just pass it in as arguments, it's better.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now