Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 675f694

Browse files
Merge pull request #48 from chhavibansal/2428_hourglass_sum
[2428] Hour Glass Sum
2 parents 625070a + 3334cb2 commit 675f694

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
For Input : [[6,2,1],[4,2,1],[9,2,8]]
7+
The Hourglass sum would be => 6 + 2 + 1 + 2( 2nd row , 2nd column) + 9 + 2 + 8
8+
9+
Return the maximum sum of the elements of an hourglass.
10+
11+
Note that an hourglass cannot be rotated and must be entirely contained within the matrix.
12+
13+
Constraints:
14+
15+
m == grid.length
16+
n == grid[i].length
17+
3 <= m, n <= 150
18+
0 <= grid[i][j] <= 106
19+
20+
Input: grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
21+
Output: 30
22+
Explanation: The cells shown above represent the hourglass with the maximum sum: 6 +たす 2 +たす 1 +たす 2 +たす 9 +たす 2 +たす 8 = 30.
23+
24+
25+
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
26+
Output: 35
27+
Explanation: There is only one hourglass in the matrix, with the sum: 1 +たす 2 +たす 3 +たす 5 +たす 7 +たす 8 +たす 9 = 35.
28+
*/
29+
30+
/**
31+
* @param {number[][]} grid
32+
* @return {number}
33+
*/
34+
var maxSum = function(grid) {
35+
const m = grid.length;
36+
const n = grid[0].length;
37+
if(m<3 || n < 3) {
38+
return 0;
39+
}
40+
let max = 0;
41+
for(let i = 0; i<m-2; i++)
42+
for(let j = 0; j<n-2;j++)
43+
{
44+
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];
45+
max = Math.max(cur, max);
46+
}
47+
return max;
48+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const assert = require('assert');
2+
const maxSum = require('../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum').maxSum;
3+
4+
function test() {
5+
assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]));
6+
assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]));
7+
}
8+
9+
module.exports.test = test

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6161
| [Verify Preorder Serialization of a Binary Tree ](/LeetcodeProblems/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ |
6262
| [Construct Binary Tree from Preorder and Inorder Traversal ](/LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js) | Medium | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ |
6363
| [Lowest Common Ancestor of a Binary Tree ](/LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
64+
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
6465
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6566
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
6667
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /