Solution: The Coin Change Problem
Explore various approaches to solving the coin change problem in detail.
We'll cover the following...
We'll cover the following...
Solution 1: Brute force
Explanation
To count the total number of solutions, we divide the solution into two sets:
- Solutions that do not contain the denomination (or
denoms[denomsLength - 1]) - Solutions that contain at least one
denoms[denomsLength - 1]
However, notice that the function recomputes the same subproblem several times (line 39).
Time complexity
The time complexity of this solution is in . Why? This is because, for every coin, we have two options: either include it or exclude it. If we think in terms of binary, it’s 0 to exclude and 1 to include. For example, if we have two coins, the options will be 00, 01, 10, 11, which is 4 or ...