From 3334cb2b268e6c7cf2dca19604323e04601244b4 Mon Sep 17 00:00:00 2001 From: Chhavi Bansal Date: Sun, 2 Oct 2022 18:13:29 +0530 Subject: [PATCH] [2428] Maximise Hour glass sum --- .../Algorithms/Maximise_Hour_Glass_Sum.js | 48 +++++++++++++++++++ .../Algorithms/Maximise_Hour_Glass_Sum.js | 9 ++++ README.md | 1 + 3 files changed, 58 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js create mode 100644 LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js diff --git a/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js b/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js new file mode 100644 index 0000000..124d4a2 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js @@ -0,0 +1,48 @@ +/* +2428. Maximum Sum of an Hourglass + +You are given an m x n integer matrix grid. +We define an hourglass as a part of the matrix with the following form: +For Input : [[6,2,1],[4,2,1],[9,2,8]] +The Hourglass sum would be => 6 + 2 + 1 + 2( 2nd row , 2nd column) + 9 + 2 + 8 + +Return the maximum sum of the elements of an hourglass. + +Note that an hourglass cannot be rotated and must be entirely contained within the matrix. + +Constraints: + +m == grid.length +n == grid[i].length +3 <= m, n <= 150 +0 <= grid[i][j] <= 106 + +Input: grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]] +Output: 30 +Explanation: The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30. + + +Input: grid = [[1,2,3],[4,5,6],[7,8,9]] +Output: 35 +Explanation: There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35. +*/ + +/** + * @param {number[][]} grid + * @return {number} + */ + var maxSum = function(grid) { + const m = grid.length; + const n = grid[0].length; + if(m<3 || n < 3) { + return 0; + } + let max = 0; + for(let i = 0; i` (e.g. | [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/ | | [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/ | | [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/ | +| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ | | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [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/ | pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy