Skip to content

Instantly share code, notes, and snippets.

@squiidz
Created November 15, 2019 20:07
Show Gist options
  • Save squiidz/30170bc73b3586af3c7130b902c6ec50 to your computer and use it in GitHub Desktop.
Save squiidz/30170bc73b3586af3c7130b902c6ec50 to your computer and use it in GitHub Desktop.
hourglass O(n)
func hourglassSum(arr [][]int32) int32 {
var maxVal int32 = -63
for i, j := 0, 0; i < len(arr) - 2; j++ {
tr := arr[i][j] + arr[i][j+1] + arr[i][j+2]
mr := arr[i+1][j+1]
br := arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]
total := tr + mr + br
if total > maxVal {
maxVal = total
}
if j == len(arr[i]) - 3 {
i += 1
j = -1
}
}
return maxVal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment