Skip to content

Commit 22be427

Browse files
author
Chhavi Bansal
committed
[2428] Hour Glass Sum
1 parent 625070a commit 22be427

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
2428. Maximum Sum of an Hourglass
3+
4+
You are given an m x n integer matrix grid.
5+
We define an hourglass as a part of the matrix with the following form:
6+
Return the maximum sum of the elements of an hourglass.
7+
8+
Note that an hourglass cannot be rotated and must be entirely contained within the matrix.
9+
Input: grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
10+
Output: 30
11+
12+
13+
*/
14+
15+
/**
16+
* @param {number[][]} grid
17+
* @return {number}
18+
*/
19+
var maxSum = function(grid) {
20+
const m = grid.length;
21+
const n = grid[0].length;
22+
if(m<3 || n < 3) {
23+
return 0;
24+
}
25+
let max = 0;
26+
for(let i = 0; i<m-2; i++)
27+
for(let j = 0; j<n-2;j++)
28+
{
29+
let cur = grid[i][j] + grid[i][j+1] + grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2];
30+
max = Math.max(cur, max);
31+
}
32+
return max;
33+
};

0 commit comments

Comments
 (0)