LeetCode 1277. Count Square Submatrices with All Ones in F#

URL

https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/?envType=daily-question&envId=2024-10-27

Code

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

let countSquares (matrix: int[,]) : int =
    let rows, cols = Array2D.length1 matrix, Array2D.length2 matrix
    let dp = Array2D.zeroCreate (rows + 1) (cols + 1)

    [ for i in 0 .. (rows - 1) do
          for j in 0 .. (cols - 1) do
              if matrix.[i, j] = 1 then
                  dp.[i + 1, j + 1] <- 1 + min dp.[i, j] (min dp.[i + 1, j] dp.[i, j + 1])
                  yield dp.[i + 1, j + 1] ]
    |> List.sum