Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Sep 22, 2023
1 parent 856149d commit 79c139c
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://leetcode.com/problems/maximum-sum-of-an-hourglass

function maxRowSum(grid, row, col) {
return (
grid[row][col] +
grid[row][col + 1] +
grid[row][col + 2] +
grid[row + 1][col + 1] +
grid[row + 2][col] +
grid[row + 2][col + 1] +
grid[row + 2][col + 2]
);
}

function maxSum(grid) {
let maxSumOfAnHourglass = 0;

for (let row = 0; row + 2 < grid.length; row++) {
for (let col = 0; col + 2 < grid[row].length; col++) {
maxSumOfAnHourglass = Math.max(
maxSumOfAnHourglass,
maxRowSum(grid, row, col)
);
}
}

return maxSumOfAnHourglass;
}

0 comments on commit 79c139c

Please sign in to comment.