ocaml: phase 5.1 count_change.ml baseline (ways to make 50c from [1;2;5;10;25] = 406)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Companion to coin_change.ml (min coins). Counts distinct multisets
via the unbounded-knapsack DP:
let count_ways coins target =
let dp = Array.make (target + 1) 0 in
dp.(0) <- 1;
List.iter (fun c ->
for i = c to target do
dp.(i) <- dp.(i) + dp.(i - c)
done
) coins;
dp.(target)
Outer loop over coins, inner DP relaxes dp.(i) += dp.(i - c). The
order matters — coin in outer, amount in inner — to count multisets
rather than ordered sequences.
count_ways [1; 2; 5; 10; 25] 50 = 406.
79 baseline programs total.
This commit is contained in:
13
lib/ocaml/baseline/count_change.ml
Normal file
13
lib/ocaml/baseline/count_change.ml
Normal file
@@ -0,0 +1,13 @@
|
||||
let count_ways coins target =
|
||||
let dp = Array.make (target + 1) 0 in
|
||||
dp.(0) <- 1;
|
||||
List.iter (fun c ->
|
||||
for i = c to target do
|
||||
dp.(i) <- dp.(i) + dp.(i - c)
|
||||
done
|
||||
) coins;
|
||||
dp.(target)
|
||||
|
||||
;;
|
||||
|
||||
count_ways [1; 2; 5; 10; 25] 50
|
||||
@@ -18,6 +18,7 @@
|
||||
"calc.ml": 13,
|
||||
"closures.ml": 315,
|
||||
"coin_change.ml": 6,
|
||||
"count_change.ml": 406,
|
||||
"csv.ml": 10,
|
||||
"exception_handle.ml": 4,
|
||||
"exception_user.ml": 26,
|
||||
|
||||
Reference in New Issue
Block a user