Advertisement

Coin Change - LeetCode 322 Solution

Coin Change - Complete Solution Guide

Coin Change is LeetCode problem 322, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 O

Detailed Explanation

The Coin Change problem asks us to find the minimum number of coins needed to make up a given target amount, given a set of coin denominations. We are allowed to use an unlimited number of each coin denomination. If it's impossible to make up the amount with the given coins, we should return -1. If the amount is 0, we return 0.

Solution Approach

The solution utilizes dynamic programming with a bottom-up approach. We create a `dp` array of size `amount + 1`. `dp[i]` stores the minimum number of coins needed to reach amount `i`. The base case is `dp[0] = 0`. We iterate through each coin denomination and, for each coin, iterate through all amounts from the coin value up to the target amount. For each amount `a`, we check if using the current coin would result in a smaller number of coins than the current value of `dp[a]`. If it does, we update `dp[a]` with the new minimum value. Finally, we return `dp[amount]` if it's less than or equal to `amount`; otherwise, we return -1, indicating that the amount cannot be made up from the given coins.

Step-by-Step Algorithm

  1. Step 1: Initialize a `dp` array of size `amount + 1` with a large value (e.g., `amount + 1`) to represent infinity. This indicates that initially, we don't know how many coins are needed for each amount.
  2. Step 2: Set `dp[0] = 0`, as it requires 0 coins to make up an amount of 0.
  3. Step 3: Iterate through each coin denomination in the `coins` array.
  4. Step 4: For each coin, iterate through the amounts from `coin` to `amount` (inclusive).
  5. Step 5: For each amount `a`, check if using the current coin can reduce the number of coins needed. This is done by comparing `dp[a]` with `dp[a - coin] + 1`. `dp[a - coin]` represents the minimum coins to reach `a - coin`, and adding 1 represents using the current coin.
  6. Step 6: If `dp[a - coin] + 1 < dp[a]`, update `dp[a]` with `dp[a - coin] + 1`. This means we've found a better way to make up the amount `a`.
  7. Step 7: After iterating through all coins and amounts, check the value of `dp[amount]`. If it's still the initial large value (indicating that we never found a way to make up the amount), return -1. Otherwise, return `dp[amount]`.

Key Insights

  • Insight 1: The problem exhibits optimal substructure; the solution to a larger problem can be constructed from solutions to smaller subproblems. This makes Dynamic Programming a suitable approach.
  • Insight 2: We can build a table (dp array) where `dp[i]` represents the minimum number of coins needed to make up the amount `i`.
  • Insight 3: Initialize the dp array with a large value (amount + 1) representing infinity to handle cases where an amount cannot be made up from the available coins. `dp[0]` is initialized to 0 because no coins are needed to make up an amount of 0.

Complexity Analysis

Time Complexity: O(n * amount)

Space Complexity: O(amount)

Topics

This problem involves: Dynamic Programming.

Companies

Asked at: Accenture, Affirm, Agoda, Airbnb, Atlassian, BlackRock, Capgemini, Capital One, ConsultAdd, Datadog, Deloitte, EPAM Systems, Geico, Infosys, Intuit, J.P. Morgan, Juniper Networks, Mastercard, Netflix, Nvidia, PayPal, Pinterest, SAP, Salesforce, Samsung, ServiceNow, Walmart Labs, Zoho, tcs.