Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Greedy BFS-frontier style — track the farthest reach within the
current jump's reachable range, and bump the jump counter when i
runs into the current frontier:
while !i < n - 1 do
farthest := max(farthest, i + arr.(i));
if !i = !cur_end then begin
jumps := !jumps + 1;
cur_end := !farthest
end;
i := !i + 1
done
For [2; 3; 1; 1; 2; 4; 2; 0; 1; 1] (n = 10), the optimal jump
sequence 0 -> 1 -> 4 -> 5 -> 9 uses 4 jumps.
Tests greedy-with-frontier pattern, three parallel refs
(jumps, cur_end, farthest), mixed for-style index loop using ref.
201 baseline programs total.
23 lines
472 B
OCaml
23 lines
472 B
OCaml
let min_jumps arr =
|
|
let n = Array.length arr in
|
|
if n <= 1 then 0
|
|
else begin
|
|
let jumps = ref 0 in
|
|
let cur_end = ref 0 in
|
|
let farthest = ref 0 in
|
|
let i = ref 0 in
|
|
while !i < n - 1 do
|
|
if !i + arr.(!i) > !farthest then farthest := !i + arr.(!i);
|
|
if !i = !cur_end then begin
|
|
jumps := !jumps + 1;
|
|
cur_end := !farthest
|
|
end;
|
|
i := !i + 1
|
|
done;
|
|
!jumps
|
|
end
|
|
|
|
;;
|
|
|
|
min_jumps [| 2; 3; 1; 1; 2; 4; 2; 0; 1; 1 |]
|