ways[0] = 1; for each coin, for a from coin to amount: ways[a] += ways[a − coin].
Approach 1
class Solution {
public int change(int amount, int[] coins) {
int[] ways = new int[amount + 1];
ways[0] = 1;
for (int c : coins)
for (int a = c; a <= amount; a++) ways[a] += ways[a - c];
return ways[amount];
}
}
Verdict: The loop order is the lesson.
Before you submit
Edge cases and common mistakes
Test these inputs
amount = 0 (1 way: no coins)
No combination (0)
Mistakes people make
Amount-outer loop (counts orderings).
Interview
Follow-up questions
How would you count combinations using each coin at most once?