You are viewing a single comment's thread. Return to all comments →
Easy to read JAVA solution:
public static int hourglassSum(List<List<Integer>> arr) { int arrLength = 6; int subArrLength = 6; int hourGlassHeight = 3; int maxSum = Integer.MIN_VALUE; int lastTopRow = arrLength - hourGlassHeight; for (int i = 0; i <= lastTopRow; i++) { for (int topCenter = 1; topCenter < subArrLength - 1; topCenter++) { List<Integer> topRow = arr.get(i); List<Integer> midRow = arr.get(i + 1); List<Integer> botRow = arr.get(i + 2); int topRowSum = ( topRow.get(topCenter - 1) + topRow.get(topCenter) + topRow.get(topCenter + 1) ); int midRowSum = midRow.get(topCenter); int botRowSum = ( botRow.get(topCenter - 1) + botRow.get(topCenter) + botRow.get(topCenter + 1) ); int currentSum = topRowSum + midRowSum + botRowSum; if (currentSum > maxSum) maxSum = currentSum; } } return maxSum; }
Seems like cookies are disabled on this browser, please enable them to open this website
2D Array - DS
You are viewing a single comment's thread. Return to all comments →
Easy to read JAVA solution: