How to Solve Coin Change Problem — Leetcode 518
Java Solution for Leetcode 518
Originally published in https://asyncq.com
Problem
- We have an infinite number of different types of coins such as coins 1, 2, 3, etc.
- We have been given coins array and amount as input. We need to form a combination from the coins array that sums up to a given amount.

Institution
- Whenever we need to find combination, optimization, min, max, most of the questions are solved by Dynamic Programming.
If you don’t know about Dynamic programming, is not that hard , it’s just people have made it buzz word and tag it as difficult.
- Simply speaking in dynamic programming we try to break down large problems into small problems to get the value for that at first and then move to bigger and bigger subproblems.
- There are two types of approaches we follow to solve any dynamic programming problem. Top Down and Bottom Up.
- In this problem, we will come up with both solution.
Solution
Top Down
- In Top-Down approach, we break larger problems into small problems. If we have to calculate the problem for f(5), then we first compute f(4). In order to calculate f(4), we first calculate f(3). Likewise, we reach to f(0) which becomes our base case.
- It’s more of a recursion/backtrack strategy. The best way to understand recursion/backtrack approach is to draw a tree diagram to understand all the options for given problem.

- In the above tree diagram, we first see all are the possibilities when the amount to be formed is 5 with a coins array. We can use coins 1, 2, and 5 and subtract them from the input amount.
- Then for each new amount (Sub Problem), we have again coined 1,2,5 and we subtract them and get a new sub-problem.
- Likewise, for a certain case, we reach up to 5 . In some cases, we overshoot the value, so basically, we cannot form 5 in that case.
- We only select cases where we can form an amount of 5 and add an increase count to return that.
Duplicates Problem
- But there is one problem since we are forming a combination, we will end up getting duplicates as well. for example [ 2,2,1] and [1,2,2] are not different combination there are same. but in our logic, we are getting duplicates.
- One of the ways to avoid duplicates is to use indexes in incremented ways. What it means is that if the current index of the coin used is 1 then it will only consider the coin at index 1, 2 .. so on and it will not consider index 0.
- In the below diagram, when we are using coin 1, we are considering all the coins. For index 2 we are only considering coins at index 2 and forward. we are not considering coins at index 1, thats how we can prevent using duplicates.

- In the above tree diagram, we can observe that there is repeated work that we are doing. For example, when the amount is 1 and the current index is 1, we are recomputing the answer, so if we use cache to keep the previously calculated answer then we can reuse it. This approach is known as Memoization.
- Below is our code for the top-down approach.









