LeetCode 200. Number of Islands in F#

URL

Number of Islands - LeetCode

Code

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

let numIslands (grid: char[,]) : int =
    let rows, cols = Array2D.length1 grid, Array2D.length2 grid

    let rec fillIsland row col =
        grid.[row, col] <- '0'

        if row >= 1 && grid.[row - 1, col] = '1' then
            fillIsland (row - 1) col

        if row + 1 < rows && grid.[row + 1, col] = '1' then
            fillIsland (row + 1) col

        if col >= 1 && grid.[row, col - 1] = '1' then
            fillIsland row (col - 1)

        if col + 1 < cols && grid.[row, col + 1] = '1' then
            fillIsland row (col + 1)

    let mutable ret = 0

    for i in 0 .. (rows - 1) do
        for j in 0 .. (cols - 1) do
            if grid.[i, j] = '1' then
                ret <- ret + 1
                fillIsland i j

    ret