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
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:
16
lib/ocaml/baseline/coin_min.ml
Normal file
16
lib/ocaml/baseline/coin_min.ml
Normal 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
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user