Files
java-interview-lab/dynamic-programming/output/04-coin-change.txt
T

19 lines
761 B
Plaintext

Coin Change: greedy, the overflow bug, call counts, loop order
==============================================================
1) Greedy fails: coins {1,3,4}, amount 6
greedy -> 3 coins (4+1+1)
DP (table) -> 2 coins (3+3)
2) 'Impossible' = Integer.MAX_VALUE, then dp[a-c] + 1: coins {2}, amount 3 (really impossible)
correct (impossible = amount + 1) -> -1
Integer.MAX_VALUE version -> -2147483648
3) Calls made: coins {1,2,5}, amount 27, answer 6
naive recursion -> 2688332 calls
memoized -> 77 calls
4) Loop order changes the question: coins {1,2,5}, amount 5
coins in the OUTER loop -> 4 (combinations: 5 / 2+2+1 / 2+1+1+1 / 1x5)
amount in the OUTER loop -> 9 (permutations: 1+2 and 2+1 now count separately)