Last active
September 2, 2020 01:18
-
-
Save evanricard/7efa7243efe5f9271f6f47c3db055100 to your computer and use it in GitHub Desktop.
hackerrank 2darr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int maxHourGlass(int[][]arr) | |
{ | |
int noRow = arr.GetUpperBound(0); | |
int noCol = arr[0].Length; | |
int max = getHourGlass(arr, 0, 0); | |
//arr has all the data now | |
//we will get a hour glass at each position excpept the last two | |
for (int i = 0; i <= noRow- 2; i++) | |
{ | |
for (int j = 0; j < noCol - 2; j++) | |
{ | |
int current = getHourGlass(arr, i, j); | |
max = Math.Max(current, max); | |
} | |
} | |
return max; | |
} | |
private static int getHourGlass(int[][] arr, int r, int c) | |
{ | |
int sum = 0; ; | |
sum+= arr[r][ c] + arr[r][ c + 1] + arr[r][ c + 2] | |
+ arr[r + 1][ c + 1] + arr[r + 2][ c] + arr[r + 2][ c + 1] | |
+ arr[r + 2][ c + 2]; | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment