322. Coin Change Leetcode Solution

Coin Change Leetcode Problem :

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return 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.

You may assume that you have an infinite number of each kind of coin.

jump game leetcode

Coin Change Leetcode Solution :

Constraints :

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 2^31 – 1
  • 0 <= amount <= 10^4

Example 1:

  • Input: coins = [2], amount = 3
  • Output: -1

Example 2:

  • Input: coins = [1], amount = 0
  • Output: 0

Intuition :

Although, the problem seems to have greedy approach as a candidate solution, this problem really needs a dynamic programming approach.

Approach :

Let c1, c2, …, ck be denominations of the k coins. Then, the amount may be arrived at by adding one more coin of the respective denomination from (amount – c1), (amount – c2), …, (amount – ck). Our job is to pick the node that needed the fewest number of coins to get to. This problem has an optimal sub-structure.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription