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.
publicstaticintsurfaceArea(List<List<Integer>>A){// Write your code hereintfront=0;intside1=0;intside2=0;intbttm=0;intback=0;inttop=0;intfrontInt=0;intsideInt=0;//Internals//Side1 and Side2side1=A.get(0).stream().mapToInt(Integer::intValue).sum();side2=A.get(A.size()-1).stream().mapToInt(Integer::intValue).sum();inti=0;for(List<Integer>list:A){intzeroCount=(int)list.stream().filter(n->n==0).count();front+=list.get(0);//Only works if no empty space..i.e filled with 0 top+=(list.size()-zeroCount);//Those with 0 heightbttm=top;back+=list.get(list.size()-1);//Calculate front and side internalsif(i>0){List<Integer>B=A.get(i-1);intmaxLength=Math.max(list.size(),B.size());sideInt+=IntStream.range(0,maxLength).map(h->{inta=h<list.size()?list.get(h):0;intb=h<B.size()?B.get(h):0;returnMath.abs(a-b);}).sum();}frontInt+=IntStream.range(1,list.size()).map(k->Math.abs(list.get(k)-list.get(k-1))).sum();i++;}System.out.printf("front: %d,side1: %d,side2: %d,back: %d, top: %d, bttm: %d, sideInt: %d, frtInt: %d",front,side1,side2,back,top,bttm,sideInt,frontInt);returnfront+side1+side2+back+top+bttm+sideInt+frontInt;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
3D Surface Area
You are viewing a single comment's thread. Return to all comments →