ocaml: phase 5.1 coin_min.ml baseline (67 cents in US coins = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s

Minimum-coin DP with -1 sentinel for unreachable values:

  let coin_min coins amount =
    let dp = Array.make (amount + 1) (-1) in
    dp.(0) <- 0;
    for i = 1 to amount do
      List.iter (fun c ->
        if c <= i && dp.(i - c) >= 0 then begin
          let cand = dp.(i - c) + 1 in
          if dp.(i) < 0 || cand < dp.(i) then dp.(i) <- cand
        end
      ) coins
    done;
    dp.(amount)

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

Tests `if c <= i && dp.(i-c) >= 0 then` short-circuit guard;
relies on iter-242 fix so dp.(i-c) is not evaluated when c > i.

158 baseline programs total.
This commit is contained in:
2026-05-10 22:07:17 +00:00
parent 3ea8967571
commit 5eed0dd5f5
3 changed files with 24 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
let coin_min coins amount =
let dp = Array.make (amount + 1) (-1) in
dp.(0) <- 0;
for i = 1 to amount do
List.iter (fun c ->
if c <= i && dp.(i - c) >= 0 then begin
let cand = dp.(i - c) + 1 in
if dp.(i) < 0 || cand < dp.(i) then dp.(i) <- cand
end
) coins
done;
dp.(amount)
;;
coin_min [1; 5; 10; 25] 67

View File

@@ -25,6 +25,7 @@
"catalan.ml": 42,
"closures.ml": 315,
"coin_change.ml": 6,
"coin_min.ml": 6,
"count_change.ml": 406,
"count_inversions.ml": 12,
"csv.ml": 10,

View File

@@ -407,6 +407,13 @@ _Newest first._
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
'a tree`) with insert + in-order traversal. Tests parametric ADT,
recursive match, List.append, List.fold_left.
- 2026-05-10 Phase 5.1 — coin_min.ml baseline (minimum-coin change
for 67¢ with US denominations = 6 coins). DP with -1 sentinel
for unreachable values: `dp.(i) := min over coins c of dp.(i-c)+1
when dp.(i-c) >= 0`. 67 = 25+25+10+5+1+1 = 6 coins. Tests `if
c <= i && dp.(i - c) >= 0 then …` guard short-circuit; without
iter-242 fix the dp.(i-c) read would crash for c > i. 158
baseline programs total.
- 2026-05-10 Phase 5.1 — flood_fill.ml baseline (largest connected
component in 5×5 grid = 7). Recursive 4-direction flood from
every unvisited 1-cell; bounds check short-circuits before the