Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Classic egg-drop puzzle DP:
dp[e][f] = 1 + min over k in [1, f] of
max(dp[e-1][k-1], dp[e][f-k])
For 2 eggs over 36 floors, the optimal worst-case is 8 trials
(closed form: triangular number bound).
Tests 2D DP with triple-nested for-loops, max-of-two via inline
if, large sentinel constant (100000000), mixed shifted indexing
(e-1) and (f-k) where both shift independently.
163 baseline programs total.
27 lines
605 B
OCaml
27 lines
605 B
OCaml
let egg_drop eggs floors =
|
|
let dp = Array.init (eggs + 1) (fun _ -> Array.make (floors + 1) 0) in
|
|
for f = 1 to floors do
|
|
dp.(1).(f) <- f
|
|
done;
|
|
for e = 1 to eggs do
|
|
dp.(e).(0) <- 0;
|
|
dp.(e).(1) <- 1
|
|
done;
|
|
for e = 2 to eggs do
|
|
for f = 2 to floors do
|
|
let best = ref 100000000 in
|
|
for k = 1 to f do
|
|
let bre = dp.(e - 1).(k - 1) in
|
|
let sur = dp.(e).(f - k) in
|
|
let cand = 1 + (if bre > sur then bre else sur) in
|
|
if cand < !best then best := cand
|
|
done;
|
|
dp.(e).(f) <- !best
|
|
done
|
|
done;
|
|
dp.(eggs).(floors)
|
|
|
|
;;
|
|
|
|
egg_drop 2 36
|