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.
For every element in the matrix, it adds top and bottom area (which are 1 unit each one), and then, for each side of every element, the area from top of the element to top of their neighbours.
For instance, in this example:
010242000
Analyzing the element '1' it will add 2 units to the area (top and bottom squares) and 1 unit for north, east and west sides (no unit at south because 4 is larger than 1). Then, for instance at element 4 it will add 2 units (top and bottom units), then 3 units at north (4-1), 2 units at east and west (4-2) and 4 units at south (4-0).
Python version:
defsurfaceArea(A):area=0neighs=[[-1,0],[0,1],[1,0],[0,-1]]forxinrange(H):foryinrange(W):el=A[x][y]# add base and top areaarea+=2fordx,dyinneighs:try:# Raise index exception if trying to access index -1if-1in[x+dx,y+dy]:raiseIndexError# Add area of actual element from neighbours as basenew=el-A[x+dx][y+dy]ifnew>0:area+=new# Add area corresponding to the sides of the figureexceptIndexError:area+=el
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 →
For every element in the matrix, it adds top and bottom area (which are 1 unit each one), and then, for each side of every element, the area from top of the element to top of their neighbours.
For instance, in this example:
Analyzing the element '1' it will add 2 units to the area (top and bottom squares) and 1 unit for north, east and west sides (no unit at south because 4 is larger than 1). Then, for instance at element 4 it will add 2 units (top and bottom units), then 3 units at north (4-1), 2 units at east and west (4-2) and 4 units at south (4-0).
Python version: