Coin Change
Coin Change is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Coin Change / Unbounded Knapsack Style) subpattern.
Problem statement
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.
Example 1:
Input: coins = [1,2,5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3 Output: -1
Example 3:
Input: coins = [1], amount = 0 Output: 0
Constraints:
- 1 <= coins.length <= 12
- 1 <= coins[i] <= 231 - 1
- 0 <= amount <= 104
Topics and companies
Topics: Array, Dynamic Programming, Breadth-First Search.
Reported in interviews at Airbnb, Amazon, Apple, BlackRock, Bloomberg, Facebook, Goldman Sachs, Google, Microsoft, Oracle, Uber, Walmart Labs.
How to practise Coin Change on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n * k), n is the number of coins, k is the amount of money time and O(k) space.
Open Coin Change in the code editor, or read the Coin Change editorial for a worked solution with its approach and complexity analysis.
Where Coin Change sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- 1D Array (Coin Change / Unbounded Knapsack Style) subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Coin Change
Other problems that use the same 1D Array (Coin Change / Unbounded Knapsack Style) and Dynamic Programming (DP) Patterns techniques:
- Coin Change II — Medium, same subpattern
- Combination Sum IV — Medium, same subpattern
- Best Time to Buy and Sell Stock — Easy, same pattern
- Best Time to Buy and Sell Stock II — Medium, same pattern
- Best Time to Buy and Sell Stock III — Hard, same pattern
- Best Time to Buy and Sell Stock IV — Hard, same pattern
Opening the problem
Progress is saved as you study.