-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHourglassSum.java
More file actions
28 lines (26 loc) · 886 Bytes
/
HourglassSum.java
File metadata and controls
28 lines (26 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class HourglassSum {
static int maxHourglassSum(int[][] mat) {
int rows = mat.length;
int cols = mat[0].length;
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i <= rows - 3; i++) {
for (int j = 0; j <= cols - 3; j++) {
int sum = mat[i][j] + mat[i][j + 1] + mat[i][j + 2]
+ mat[i + 1][j + 1]
+ mat[i + 2][j] + mat[i + 2][j + 1] + mat[i + 2][j + 2];
maxSum = Math.max(maxSum, sum);
}
}
return maxSum;
}
public static void main(String[] args) {
int[][] mat = {
{1, 1, 1, 0, 0},
{0, 1, 0, 0, 0},
{1, 1, 1, 0, 0},
{0, 9, 2, -4, -4},
{0, 0, 0, -2, 0}
};
System.out.println("Maximum Hourglass Sum: " + maxHourglassSum(mat));
}
}