322. Coin Change

Coin Change (LeetCode 322) 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.

How to solve Coin Change

Coin Change is a Coin Change problem, part of Dynamic Programming (DP) Patterns. Solve coin change and unbounded knapsack problems where items can be reused.

Work towards the solution in these steps:

  1. Consider using dynamic programming to systematically build up solutions for smaller amounts to solve for the target amount.
  2. Think about defining a 1D array where each index represents the minimum coins needed to make that amount, initializing it appropriately.
  3. Iterate through the array amounts from 1 to the target, and for each, check all coin denominations to update the minimum coins required.

The reference solution runs in O(n * k) time and O(k) space.

The Coin Change solution and editorial walks through the full approach with reference code.

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] <= 2^31 - 1

- 0 <= amount <= 10^4

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) 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

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 FAQ

What is the LeetCode number of Coin Change?

Coin Change is LeetCode problem 322, rated Medium.

What pattern is used to solve Coin Change?

Coin Change is a Coin Change problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Coin Change / Unbounded Knapsack Style) subpattern.

What is the time complexity of Coin Change?

The reference solution runs in O(n * k) time and O(k) space.

Which companies ask Coin Change?

Coin Change has been reported in interviews at Airbnb, Amazon, Apple, BlackRock, Bloomberg, Facebook, Goldman Sachs, Google, Microsoft, Oracle, Uber, Walmart Labs.

Preparing this workspace.