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.
My Java solution with o(n * m) time complexity and o(1) space complexity:
publicstaticinthourglassSum(List<List<Integer>>arr){if(arr.size()<3)return0;//no possible hourglassintmax=Integer.MIN_VALUE;//iterate over each row except the last twointi=0;while(i<arr.size()-2){//iterate over each col except the first & lastintj=1;while(j<arr.get(i).size()-1){intcurrMax=calcHourglassSum(arr,i,j);//get the curr maxmax=Math.max(currMax,max);//update the maxj++;}i++;}returnmax;}publicstaticintcalcHourglassSum(List<List<Integer>>arr,inti,intj){returnarr.get(i).get(j)+arr.get(i).get(j-1)+arr.get(i).get(j+1)+arr.get(i+1).get(j)+arr.get(i+2).get(j)+arr.get(i+2).get(j-1)+arr.get(i+2).get(j+1);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
2D Array - DS
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n * m) time complexity and o(1) space complexity: