MEDIUM NC#106 Blind #17 Dynamic Programming 1D
322. Coin Change
📖 Problem
Given an array of coins of different denominations and a target amount, return the fewest number of coins needed to make up that amount. Return -1 if impossible.
🧠 Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- •Array methods (`push`, `pop`, `shift`, `slice`)
- •Object/Map/Set usage patterns
- •Function parameter and return typing
- •Array DP table updates
- •State transition thinking
- •Base case initialization
Logical Thinking Concepts
- •Define invariants before coding
- •Check edge cases first (`[]`, single element, duplicates)
- •Estimate time/space before implementation
- •Apply Dynamic Programming reasoning pattern
- •Apply BFS reasoning pattern
- •Apply Backtracking reasoning pattern
💡 Approach
- → dp[i] = min coins to make amount i
- → For each coin, update dp for all amounts >= coin
- → dp[i] = min(dp[i], dp[i-coin] + 1)
- → Time: O(n * amount), Space: O(amount)
🧭 Prerequisites
🛠️ Hints & Pitfalls
Hints
- •dp[i] = min coins to make amount i
- •For each coin, update dp for all amounts >= coin
- •dp[i] = min(dp[i], dp[i-coin] + 1)
Common Pitfalls
- •Time: O(n * amount), Space: O(amount)
🧪 Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
coinChange([1,2,5], 11); Expected:
3 Test Case 2
Not run Input:
coinChange([2], 3); Expected:
-1 Test Case 3
Not run Input:
coinChange([1], 0); Expected:
0 📝 Code Editor
📤 Output