Coin Change Problem

viaGlassdoor

Problem: Given an array of coin denominations and a target amount, find the minimum number of coins needed to make up that amount (or determine it's not possible).

Constraints: Unlimited supply of each denomination; return -1 if the amount cannot be formed.

Example: coins = [1,2,5], amount = 11 -> minimum coins = 3 (5+5+1).

Approach: Dynamic programming - build a dp array of size amount+1 where dp[i] is the minimum coins to make amount i, initialized to infinity except dp[0]=0. For each amount i from 1 to target, for each coin c <= i, dp[i] = min(dp[i], dp[i-c]+1). Final answer is dp[amount] (or -1 if still infinity). O(amount * numCoins) time.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account