LeetCode 304. Range Sum Query 2D - Immutable in F#

URL

leetcode.com/problems/range-sum-query-2d-im..

Code

github.com/syohex/dotnet-study/blob/master/..

type NumMatrix =
    { Acc: int [,] }

    static member init(matrix: int [,]) : NumMatrix =
        let rows = Array2D.length1 matrix
        let cols = Array2D.length2 matrix

        let acc = Array2D.zeroCreate (rows + 1) cols

        for i in 0 .. (rows - 1) do
            for j in 0 .. (cols - 1) do
                acc.[i + 1, j] <- acc.[i, j] + matrix.[i, j]

        { Acc = acc }

    member this.sumRegion (row1: int) (col1: int) (row2: int) (col2: int) : int =
        seq { col1 .. col2 }
        |> Seq.fold (fun acc n -> acc + this.Acc.[row2 + 1, n] - this.Acc.[row1, n]) 0