You are viewing a single comment's thread. Return to all comments →
public static int hourglassSum(List<List<Integer>> arr) { // Write your code here List<Integer> upper=new ArrayList<>(); List<Integer> middle=new ArrayList<>(); List<Integer> bottom=new ArrayList<>(); //------------------ //upper for(int i=0;i<arr.size()-2;i++){ int j=0; int sum=0; int rotation =1; int count=0; while(j<arr.size()){ sum += arr.get(i).get(j); j++; count++; if(count==3){ j =rotation; rotation++; count=0; upper.add(sum); sum=0; } if(rotation>4){ break; } } } ///--------------------------- //Middle for(int i=1;i<arr.size()-1;i++) for(int j=1;j<arr.size()-1;j++){ middle.add(arr.get(i).get(j)); } //------------------------------ //Lower for(int i=2;i<arr.size();i++){ int j=0; int sum=0; int rotation =1; int count=0; while(j<arr.size()){ sum += arr.get(i).get(j); j++; count++; if(count==3){ j =rotation; rotation++; count=0; bottom.add(sum); sum=0; } if(rotation>4){ break; } } } // calculet //--------------------------- List<Integer> result=new ArrayList<>(); int addition=0; for(int i=0;i<upper.size();i++){ addition=upper.get(i)+middle.get(i)+bottom.get(i); result.add(addition); addition=0; } // System.out.println(upper); // System.out.println(middle); // System.out.println(bottom); // System.out.println(result); return result.stream().max(Integer:: compare).orElse(0); }
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 →
Java