We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defhourglassSum(arr):# determine a value outside of the smallest possible# hourglass sum and use as the starting max_summin_arr_val=-9max_sum=(min_arr_val-1)*7max_hourglass_starting_index=4foriinrange(max_hourglass_starting_index):# get sum of initial hourglass for row i# top row sum, bottom row sum, center valuecurr_sum=sum(arr[i][:3])+sum(arr[i+2][:3])+arr[i+1][1]max_sum=max(max_sum,curr_sum)forjinrange(1,max_hourglass_starting_index):# sliding window through columns 0-3# top row sumcurr_sum-=arr[i][j-1]curr_sum+=arr[i][j+2]# bottom row sumcurr_sum-=arr[i+2][j-1]curr_sum+=arr[i+2][j+2]# center valuecurr_sum-=arr[i+1][j]curr_sum+=arr[i+1][j+1]max_sum=max(max_sum,curr_sum)returnmax_sum
Cookie support is required to access HackerRank
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 →
Python Sliding Window-esque approach