Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 2428. Maximum Sum of an Hourglass in F#

Shohei Yoshida's photo
Shohei Yoshida
·Oct 12, 2022·

1 min read

URL

leetcode.com/problems/maximum-sum-of-an-hou..

Code

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

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

    let mutable ret = 0

    for i in 0 .. (rows - 3) do
        for j in 0 .. (cols - 3) do
            let mutable sum = 0

            for x in 0..2 do
                for y in 0..2 do
                    if not ((x = 1 && y = 0) || (x = 1 && y = 2)) then
                        sum <- sum + grid.[i + x, j + y]

            ret <- System.Math.Max(ret, sum)

    ret
 
Share this