LeetCode 1380. Lucky Numbers in a Matrix in F#
URL
Lucky Numbers in a Matrix - LeetCode
Code
let luckyNumbers (matrix: int[,]) : int list =
let rows, cols = Array2D.length1 matrix, Array2D.length2 matrix
let rowMins =
seq { 0 .. (rows - 1) }
|> Seq.map (fun i -> Array.min matrix.[i, *])
|> Seq.toArray
let colMaxs =
seq { 0 .. (cols - 1) }
|> Seq.map (fun i -> Array.max matrix.[*, i])
|> Seq.toArray
let rec luckeyNumbers' i j (matrix: int[,]) acc =
if i >= rows then
acc
elif j >= cols then
luckeyNumbers' (i + 1) 0 matrix acc
else
let v = matrix.[i, j]
let acc = if v = rowMins.[i] && v = colMaxs.[j] then v :: acc else acc
luckeyNumbers' i (j + 1) matrix acc
luckeyNumbers' 0 0 matrix []