# Dynamic programming: speed of top down vs bottom up approaches

I have just completed a dynamic programming exercise on LeetCode (Coin Change). I tried a top down approach, but it failed for the larger inputs, whereas the bottom up approach worked for all inputs. Is the top down approach significantly slower because of the recursion?

I thought that bottom up approaches needed to compute everything: eg if we have a result array of size n, we run a loop from i=0 to i=n to find result[i], whereas the top down approach only computes relevant entries (we start at i=n then go down to 0, but we might skip some of the entries in between). So I thought the top down approach would be faster.

Or maybe there was just a bug in my top down approach.

Update: The problem on LeetCode is: " You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1: coins = [1, 2, 5], amount = 11 return 3 (11 = 5 + 5 + 1)

Example 2: coins = , amount = 3 return -1."

I defined an array result of size amount+1; result[i] is the minimum number of coins needed to reach amount i (with result = 0).

If we have N coins, and coin j has value V[j], the recursion is: result[i] = 1 + minimum of result[i-V[j]] for all j=1...N (only the j for which i-V[j]>=0)

I used a bottom up and top down approach to compute result[amount]:

If we don't have a coin of value 1, we won't need to compute result[i] for every i with a top down approach.

The time limit was exceeded for top down with this input: Coins: [186,419,83,408], Amount: 6249

• What exactly are the algorithms you are implementing? What are the inputs? – Raphael Dec 27 '15 at 15:24
• Top down can be faster if the relevant entries comprise a relatively small part of the output space, but it has some overhead compared to bottom up. If your problem is such that you need almost all the entries anyway, bottom up might be faster. You haven't really provided enough information to say anything more meaningful. – Tom van der Zanden Dec 27 '15 at 15:46
• Until we see your code I'll just bet on its being buggy. That's pretty much a safe bet all the time regarding any code. – Andrej Bauer Aug 16 '18 at 17:32
• @AndrejBauer Exactly right. With the numbers involved, straightforward, unoptimised, but correct top-down approach should find the solution very, very quickly. – gnasher729 Oct 16 '18 at 8:53