2428. Maximum Sum of an Hourglass Medium
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 * <img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/img.jpg" style="width: 243px; height: 243px;" />
7 * Return the maximum sum of the elements of an hourglass.
8 * Note that an hourglass cannot be rotated and must be entirely contained within the matrix.
9 *
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/1.jpg" style="width: 323px; height: 323px;" />
12 * Input: grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
13 * Output: 30
14 * Explanation: The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.
15 *
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/2.jpg" style="width: 243px; height: 243px;" />
18 * Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
19 * Output: 35
20 * Explanation: There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.
21 *
22 *
23 * Constraints:
24 *
25 * m == grid.length
26 * n == grid[i].length
27 * 3 <= m, n <= 150
28 * 0 <= grid[i][j] <= 10^6
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/maximum-sum-of-an-hourglass/
34// discuss: https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn max_sum(grid: Vec<Vec<i32>>) -> i32 {
40 0
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_2428() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.