</>

332. Coin Challenge

Medium NeetCode 150
1-D Dynamic Programming Time O(n*m) Space O(n)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
1-D Dynamic Programming
Time
O(n*m)
Space
O(n)

Standard Solution (C++)

SummaryGiven a set of coin denominations and a target amount, find the minimum number of coins needed to make exact change for that amount, or return -1 if no combination can reach it.

Approach
  • Use bottom-up 1-D DP where dp[i] holds the minimum coins needed to form amount i.
  • Initialize dp[0] = 0 (zero coins for zero amount) and every other entry to amount + 1 as a sentinel for "unreachable."
  • For each sub-amount from 1 to amount, try every coin: if the coin fits, update dp[i] = min(dp[i], dp[i - coin] + 1).
  • After filling the table, any entry still exceeding amount is truly unreachable, so return -1 in that case.
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        // dp[i] = fewest coins to reach exactly amount i
        // sentinel: amount+1 is larger than any valid answer
        vector<int> dp(amount + 1, amount + 1);
        dp[0] = 0;  // base case: 0 coins needed for amount 0

        for (int i = 1; i <= amount; ++i) {
            for (int coin : coins) {
                if (coin <= i) {
                    dp[i] = min(dp[i], dp[i - coin] + 1);
                }
            }
        }

        // if dp[amount] is still the sentinel, the amount is unreachable
        return dp[amount] > amount ? -1 : dp[amount];
    }
};
Time O(amount * n) where n is the number of coin denominations Space O(amount)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

#include <vector>
#include <algorithm>

class Solution {
public:
    int coinChange(std::vector<int>& coins, int amount) {
        // build a vector, to record the fewest num of coins to make up the corresponding amount
        std::vector<int> dp(amount+1, amount+1);
        // base case
        dp[0]=0;

        /* not necessary
        for(int deno: coins){
            if(deno<=amount){
                dp[deno]=1;
            }
        }
        */

        for(int i=1; i<amount+1; i++){
            for(int deno: coins){
                if(i-deno>=0)
                    dp[i]=std::min(dp[i-deno]+1, dp[i]);
            }
        }

        return (dp[amount]>amount)? -1 : dp[amount];
    }
};

// Time Complexity: O(n*m) where n is the amount and m is the number of coins
// Space Complexity: O(n)
Time O(n*m) Space O(n)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern