Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Two utility functions:
prefix_sums xs builds Array of len n+1 such that
arr.(i) = sum of xs[0..i-1]
range_sum p lo hi = p.(hi+1) - p.(lo)
For [3;1;4;1;5;9;2;6;5;3]:
range_sum 0 4 = 14 (3+1+4+1+5)
range_sum 5 9 = 25 (9+2+6+5+3)
range_sum 2 7 = 27 (4+1+5+9+2+6)
sum = 66
Tests List.iter mutating Array indexed by a ref counter, plus the
classic prefix-sum technique for O(1) range queries.
67 baseline programs total.
17 lines
367 B
OCaml
17 lines
367 B
OCaml
let prefix_sums xs =
|
|
let n = List.length xs in
|
|
let arr = Array.make (n + 1) 0 in
|
|
let i = ref 0 in
|
|
List.iter (fun x ->
|
|
arr.(!i + 1) <- arr.(!i) + x;
|
|
i := !i + 1
|
|
) xs;
|
|
arr
|
|
|
|
let range_sum prefixes lo hi = prefixes.(hi + 1) - prefixes.(lo)
|
|
|
|
;;
|
|
|
|
let p = prefix_sums [3; 1; 4; 1; 5; 9; 2; 6; 5; 3] in
|
|
range_sum p 0 4 + range_sum p 5 9 + range_sum p 2 7
|