LeetCode 2661. First Completely Painted Row or Column in F#

URL

First Completely Painted Row or Column - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202501/first_completely_painted_row_or_column/main.fsx

let firstCompleteIndex (arr: int list) (mat: int[,]) : int =
    let rows, cols = Array2D.length1 mat, Array2D.length2 mat

    let rec firstCompleteIndex' arr i (rowSum: int[]) (colSum: int[]) valueMap =
        match arr with
        | [] -> failwith "never reach here"
        | h :: t ->
            let row, col = Map.find h valueMap
            rowSum.[row] <- rowSum.[row] + 1
            colSum.[col] <- colSum.[col] + 1

            if rowSum.[row] = cols || colSum.[col] = rows then
                i
            else
                firstCompleteIndex' t (i + 1) rowSum colSum valueMap

    let valueMap =
        seq {
            for i in 0 .. (rows - 1) do
                for j in 0 .. (cols - 1) do
                    yield i, j, mat.[i, j]
        }
        |> Seq.fold (fun acc (i, j, v) -> Map.add v (i, j) acc) Map.empty

    let rowSum, colSum = Array.zeroCreate rows, Array.zeroCreate cols
    firstCompleteIndex' arr 0 rowSum colSum valueMap