Maximum sum of hour glass in matrix in Golang

Jafar P
Apr 23, 2024
func hourglassSum(arr [][]int32) int32 {
maxSum := int32(math.MinInt32)
for i, row := range arr{
if i == len(arr)-2{
break
}
for idx, _ := range row{
sum := int32(0)
if idx == len(row)-2{
break
}
for start := idx; start < idx+3; start++{
sum = sum + arr[i][start] + arr[i+2][start]
}
sum = sum + arr[i+1][idx+1]

if maxSum < sum {
maxSum = sum
}
}
}
return int32(maxSum)
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jafar P
Jafar P

No responses yet

Write a response