← Salesforce Interview Insights
The core insight is framing dp[i] around the amount, not some index into the coins array.
Use dynamic programming to compute the minimum coins for each amount from 0 to target, building up from smaller subproblems. For each amount, consider each coin and take the minimum of 1 + dp[amount - coin] if that subproblem is solvable. Return dp[target] or -1 if unreachable.
Pro tip: Mention that this is the classic unbounded knapsack/coin change problem, and that BFS on the amount graph can also solve it in O(target * number of coins) time. Also note that if the coin system is canonical (like US coins), a greedy approach works, but it's not guaranteed for arbitrary denominations.
Restate the problem to ensure understanding: distinct denominations, unlimited reuse, return minimum coins or -1. Ask about constraints (e.g., target size, number of coins) to guide algorithm choice.
Decide between top-down memoization and bottom-up tabulation. Explain that DP is needed because greedy fails for arbitrary denominations.
Let dp[i] be the minimum coins to make amount i. Initialize dp[0]=0 and dp[i]=infinity for i>0. For each i from 1 to target, dp[i] = min(dp[i - coin] + 1) over all coins ≤ i.
Code the DP iteratively. After filling, return dp[target] if it's not infinity, else -1. Handle target=0 (return 0) and empty coins array.
State time complexity O(target * number of coins) and space O(target). Walk through a small example to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.