Rust Frog Jmp
fn frog_jmp(x: i64, y: i64, d: i64) -> i64 {
let dist = y - x;
(dist + d - 1) / d
}
This computes the jump count with math instead of simulation, which is the cleanest way to solve it.
TypeScript Frog Jmp
function frogJmp(x: number, y: number, d: number): number {
return Math.ceil((y - x) / d);
}
This computes the jump count with math instead of simulation, which is the cleanest way to solve it.
Bash Frog River One
frog_river_one() {
local _x=$1
local -n _a="$2"
local -A _existing
local _k _v
for _k in "${!_a[@]}"; do
_v=${_a[$_k]}
if [[ -z "${_existing[$_v]:-}" ]] && (( _v <= _x )); then
_existing[$_v]=1
if (( ${#_existing[@]} == _x )); then
echo "$_k"
return
fi
fi
done
echo -1
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
C++ Frog River One
#include <cstddef>
#include <vector>
int frogRiverOne(int x, const std::vector<int>& a)
{
std::vector<bool> existing(x + 1, false);
int count = 0;
for (std::size_t k = 0; k < a.size(); ++k) {
int i = a[k];
if (i <= x && !existing[i]) {
existing[i] = true;
++count;
if (count == x) {
return static_cast<int>(k);
}
}
}
return -1;
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
C# Frog River One
static int FrogRiverOne(int x, int[] a)
{
var existing = new HashSet<int>();
for (int k = 0; k < a.Length; k++)
{
var i = a[k];
if (i <= x && existing.Add(i) && existing.Count == x)
{
return k;
}
}
return -1;
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Elixir Frog River One
defmodule FrogRiverOne do
def frog_river_one(x, a) do
a
|> Enum.with_index()
|> Enum.reduce_while(MapSet.new(), fn {leaf, k}, seen ->
seen =
if leaf <= x and not MapSet.member?(seen, leaf) do
MapSet.put(seen, leaf)
else
seen
end
if MapSet.size(seen) == x, do: {:halt, k}, else: {:cont, seen}
end)
|> case do
k when is_integer(k) -> k
_ -> -1
end
end
end
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Erlang Frog River One
-module(frog_river_one).
-export([frog_river_one/2]).
frog_river_one(X, A) ->
frog_river_one(X, A, 0, sets:new()).
frog_river_one(_X, [], _K, _Seen) ->
-1;
frog_river_one(X, [H | T], K, Seen) ->
case H =< X andalso not sets:is_element(H, Seen) of
true ->
Seen1 = sets:add_element(H, Seen),
case sets:size(Seen1) =:= X of
true -> K;
false -> frog_river_one(X, T, K + 1, Seen1)
end;
false ->
frog_river_one(X, T, K + 1, Seen)
end.
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Go Frog River One
func frogRiverOne(x int, a []int) int {
existing := make(map[int]bool)
for k, v := range a {
if v <= x && !existing[v] {
existing[v] = true
if len(existing) == x {
return k
}
}
}
return -1
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Haskell Frog River One
import qualified Data.Set as Set
frogRiverOne :: Int -> [Int] -> Int
frogRiverOne x a = go (zip [0 ..] a) Set.empty
where
go [] _ = -1
go ((k, i) : rest) seen
| i <= x && not (Set.member i seen) =
let seen' = Set.insert i seen
in if Set.size seen' == x then k else go rest seen'
| otherwise = go rest seen
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Java Frog River One
public class Solution {
public static int frogRiverOne(int x, int[] a) {
boolean[] existing = new boolean[x + 1];
int count = 0;
for (int k = 0; k < a.length; k++) {
int i = a[k];
if (i <= x && !existing[i]) {
existing[i] = true;
count++;
if (count == x) {
return k;
}
}
}
return -1;
}
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.