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.
17 lines
353 B
OCaml
17 lines
353 B
OCaml
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
|