PHP Frog River One
function frogRiverOne(int $x, array $a): int
{
$existing = [];
foreach ($a as $k => $i) {
if ( ! isset($existing[$i]) && $i <= $x) {
$existing[$i] = $i;
if (count($existing) === $x) {
return $k;
}
}
}
return -1;
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.