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.
Code in JAVA 8.
The way this work is to calculate the front, rear and corner bottom-right of each tile, then add the limits (top of first row and left of first column)
publicstaticintsurfaceArea(List<List<Integer>>A){//Initiate valuesintH=A.size();intW=A.get(0).size();intprice=0;////If we look up a block, facing the grid, we call each of it's side ://// top// left right// bottom////The side facing us it called front, it's opposite is the rear//////We will go through each block. The area that we calculate the area of the corner created by this piece //and the one below and on its right side. We also add the front and rear area.//With that method we get every 1x1 area except the top of the first row and the left of the first collumnfor(inti=0;i<H;i++){for(intj=0;j<W;j++){//We will input the difference in height between the current block and the one below and to it's rightintcurrentBlock=A.get(i).get(j);intrightBlock=0;intbottomBlock=0;if(i!=H-1){bottomBlock=A.get(i+1).get(j);}if(j!=W-1){rightBlock=A.get(i).get(j+1);}// We add 1 for the tfront face, and one for the rear faceprice+=Math.abs(currentBlock-rightBlock)+Math.abs(currentBlock-bottomBlock)+2;}//We add the top of the first rowprice+=A.get(i).get(0);}//We add the left of the first collumnfor(intj=0;j<W;j++){price+=A.get(0).get(j);}returnprice;}
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 →
Code in JAVA 8. The way this work is to calculate the front, rear and corner bottom-right of each tile, then add the limits (top of first row and left of first column)