Created
May 26, 2019 10:42
-
-
Save netchampfaris/6f5e76cd7527bdc88de95878d3b3405e to your computer and use it in GitHub Desktop.
Hackerrank Solution to https://www.hackerrank.com/challenges/2d-array/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays
This file contains hidden or 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
arr = [ | |
[1, 1, 1, 0, 0, 0], | |
[0, 1, 0, 0, 0, 0], | |
[1, 1, 1, 0, 0, 0], | |
[0, 0, 2, 4, 4, 0], | |
[0, 0, 0, 2, 0, 0], | |
[0, 0, 1, 2, 4, 0], | |
] | |
def get_hour_glass(i, j): | |
hour_glass_indices = [ | |
(i, j), | |
(i, j + 1), | |
(i, j + 2), | |
(i + 1, j + 1), | |
(i + 2, j), | |
(i + 2, j + 1), | |
(i + 2, j + 2) | |
] | |
return sum([arr[m][n] for m, n in hour_glass_indices]) | |
def get_max_sum(): | |
max_sum = -2**10 | |
for i in range(0, len(arr) - 2): | |
n = len(arr[i]) - 2 | |
for j in range(0, n): | |
hour_glass_sum = get_hour_glass(i, j) | |
if hour_glass_sum > max_sum: | |
max_sum = hour_glass_sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!