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.
A Javascript solution using O(n*m) time complexity and O(1) space complexity.
functionhourGlass(arr){// we could set this to 3 given the problems constraings, but this allows changesmaxX=3;// + (arr[0].length % 3)maxY=3;// + (arr.length % 3)total=-Infinity;// has to be -64, but// begin at y == 0for(lety=0;y<=maxY;y++){for(letx=0;x<=maxX;x++){// sum the top of hourglassletsum=arr[y][x]+arr[y][x+1]+arr[y][x+2];// get the middle of hourglasssum+=arr[y+1][x+1];// sum the bottom of hourglasssum+=arr[y+2][x]+arr[y+2][x+1]+arr[y+2][x+2]// don't store result to keep space complexity downif(total<sum)total=sum;}}returntotal;}
2D Array - DS
You are viewing a single comment's thread. Return to all comments →
A Javascript solution using
O(n*m)
time complexity andO(1)
space complexity.