Add dynamic-programming module: ten classic DP problems, memoization vs tabulation vs space-optimised, call-count and pitfall transcripts (11 tests)

This commit is contained in:
2026-11-09 08:41:01 +00:00
parent 911aef5515
commit cb1b28682f
33 changed files with 1551 additions and 0 deletions
@@ -0,0 +1,18 @@
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)