From 79c139c88e4b8e6d5d6365565a92f77c41dcb184 Mon Sep 17 00:00:00 2001 From: TK Date: Fri, 22 Sep 2023 09:47:03 -0300 Subject: [PATCH] * --- .../maximum-sum-of-an-hourglass.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 coding_interviews/leetcode/medium/maximum-sum-of-an-hourglass/maximum-sum-of-an-hourglass.js diff --git a/coding_interviews/leetcode/medium/maximum-sum-of-an-hourglass/maximum-sum-of-an-hourglass.js b/coding_interviews/leetcode/medium/maximum-sum-of-an-hourglass/maximum-sum-of-an-hourglass.js new file mode 100644 index 0000000..df09e47 --- /dev/null +++ b/coding_interviews/leetcode/medium/maximum-sum-of-an-hourglass/maximum-sum-of-an-hourglass.js @@ -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; +}