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
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:
15
lib/ocaml/baseline/coin_change.ml
Normal file
15
lib/ocaml/baseline/coin_change.ml
Normal file
@@ -0,0 +1,15 @@
|
||||
let coin_change coins target =
|
||||
let dp = Array.make (target + 1) (target + 1) in
|
||||
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;
|
||||
if dp.(target) > target then -1
|
||||
else dp.(target)
|
||||
|
||||
;;
|
||||
|
||||
coin_change [1; 5; 10; 25] 67
|
||||
@@ -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,
|
||||
|
||||
@@ -407,6 +407,14 @@ _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-09 Phase 5.1 — coin_change.ml baseline (min coin DP, 67¢
|
||||
with [1;5;10;25] → 6 coins). Bottom-up DP: `dp[i]` = min coins
|
||||
for amount i. For each amount 1..target, iterate coins and
|
||||
relax `dp[i] = min(dp[i], dp[i-c] + 1)`. Sentinel `target + 1`
|
||||
represents "impossible" since any real solution uses at most
|
||||
target coins. 67 = 25+25+10+5+1+1 = 6 coins. Exercises
|
||||
Array.make + arr.(i) + arr.(i) <- v + nested for/List.iter +
|
||||
guard `c <= i`. 47 baseline programs total.
|
||||
- 2026-05-09 Phase 5.1 — kadane.ml baseline (Kadane's max subarray
|
||||
sum = 6). Classic O(n) algorithm using two refs and `max`. For
|
||||
[-2;1;-3;4;-1;2;1;-5;4] the optimal subarray is [4;-1;2;1] = 6.
|
||||
|
||||
Reference in New Issue
Block a user