ocaml: phase 5.1 coin_change.ml baseline (DP, 67c with [1;5;10;25] = 6 coins)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s

Bottom-up dynamic programming. dp[i] = minimum coins to make
amount i.

  let dp = Array.make (target + 1) (target + 1) in   (* sentinel *)
  dp.(0) <- 0;
  for i = 1 to target do
    List.iter (fun c ->
      if c <= i && dp.(i - c) + 1 < dp.(i) then
        dp.(i) <- dp.(i - c) + 1
    ) coins
  done

Sentinel 'target + 1' means impossible — any real solution uses at
most 'target' coins.

  coin_change [1; 5; 10; 25] 67   = 6   (= 25+25+10+5+1+1)

Exercises Array.make + arr.(i) + arr.(i) <- v + nested
for/List.iter + guard 'c <= i'.

47 baseline programs total.
This commit is contained in:
2026-05-09 10:21:11 +00:00
parent b3d5da5361
commit aee7226b9c
3 changed files with 24 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
"caesar.ml": 215,
"calc.ml": 13,
"closures.ml": 315,
"coin_change.ml": 6,
"csv.ml": 10,
"exception_handle.ml": 4,
"expr_eval.ml": 16,