LeetCode 2684. Maximum Number of Moves in a Grid in F#
URL
Maximum Number of Moves in a Grid - LeetCode
Code
let maxMoves (grid: int[,]) : int =
let rows, cols = Array2D.length1 grid, Array2D.length2 grid
let isValid x y =
x >= 0 && x < rows && y >= 0 && y < cols
let steps = [ (-1, 1); (0, 1); (1, 1) ]
let rec maxMoves' q acc visited =
if Set.isEmpty q then
acc
else
let acc' = q |> Set.fold (fun acc (_, j) -> max acc j) acc
let q' =
q
|> Seq.fold
(fun acc (i, j) ->
steps
|> List.map (fun (x, y) -> i + x, j + y)
|> List.filter (fun (x, y) -> isValid x y && not <| Set.contains (x, y) visited)
|> List.filter (fun (x, y) -> grid.[i, j] < grid.[x, y])
|> List.fold (fun q pos -> Set.add pos q) acc)
Set.empty
let visited' = Set.union visited q
maxMoves' q' acc' visited'
let q =
seq { 0 .. (rows - 1) } |> Seq.fold (fun acc i -> Set.add (i, 0) acc) Set.empty
maxMoves' q 0 Set.empty