Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
DP filling a flattened 2D array: dp.(0, 0) = 1 dp.(i, j) = dp.(i-1, j) + dp.(i, j-1) index = i * (n+1) + j For a 4x6 grid (5x7 dp matrix), the count is C(10, 4) = 210. Tests Array as 2D via row-major flatten + nested for + multi-step conditional access (above/left guarded by 'if i > 0' / 'if j > 0'). 87 baseline programs total.
18 lines
416 B
OCaml
18 lines
416 B
OCaml
let count_paths m n =
|
|
let dp = Array.make ((m + 1) * (n + 1)) 0 in
|
|
dp.(0) <- 1;
|
|
for i = 0 to m do
|
|
for j = 0 to n do
|
|
if i > 0 || j > 0 then begin
|
|
let above = if i > 0 then dp.((i - 1) * (n + 1) + j) else 0 in
|
|
let left = if j > 0 then dp.(i * (n + 1) + j - 1) else 0 in
|
|
dp.(i * (n + 1) + j) <- above + left
|
|
end
|
|
done
|
|
done;
|
|
dp.(m * (n + 1) + n)
|
|
|
|
;;
|
|
|
|
count_paths 4 6
|