Surface area of the three-dimensional volume LeetCode-

Subject description:

N * N on the grid, we put some cube 1 * 1 * 1.

Each value v = grid [i] [j] denotes the v cube superposed on the corresponding cell (i, j).

Please return to the final body surface area.

Example:

Input: [[2]] 
Output: 10
Input: [[1,2], [3,4]] 
Output: 34
Input: [[1,0], [0,2 ]] 
Output: 16
Input: [[1,1,1], [1,0,1], [1,1,1 ]] 
Output: 32
Input: [[2,2,2], [2,1,2], [2,2,2 ]] 
Output: 46

Problem-solving ideas:

Questions have not read ~ (I first saw the title of the idea). Explain the meaning of the expression of the input sample.

Example 1: put two cubes at the coordinates (0,0) in the cell.

Example 2: put a cube at coordinates (0,0) in the cell. Put two cube coordinate (0,1) of the cell. Cell coordinates (1,0) in the discharge 3 cubes. Cell coordinates (1,1) in the discharge four cubes.

....

Then look at this question, in fact, the subject of the meaning of these cubes is obtained in addition to other parts of the surface area of ​​the covered part. Examples or the input sample.

Example 1: (0,0) has two cubes, this pile with two cubes (one placed above the other), which has two faces to be blocked, the surface area of ​​the other portion (2 * 6 - 2 * 1 => two cubes, each having six faces, two covered faces, each a square) = 10.

Example 2: A total of 10 cubes, the surface 26 is covered, the surface area of ​​the other portion (10 * 6--26 * 1) = 34.

....

Look at the code will understand a little more thoroughly. Also be sure to draw their own map on paper.

Code:

Solution class: 
DEF surfacearea (Self, Grid: List [List [int]]) -> int:
IF len (Grid) == 0:
return 0
Block = 0 # How many total statistics cube
cover = 0 #统计有多少个面被其他面盖住,在所有的立方体的表面积上减去被盖住的面数×2

for I in Range (len (Grid)):
for J in Range (len (Grid [I])):
IF Grid [I] [J]> 0:
Block + = Grid [I] [J]
Cover + = Grid [I] [J] -. 1
IF I> 0:
Cover + = min (grid [I -. 1] [J], grid [I] [J]) on the same row #, comparing two cells stacked the number of cubes, that take a small
IF J> 0:
Cover + = min (Grid [I] [J -. 1], Grid [I] [J]) # Similarly
return block * 6 - cover * 2

 

Guess you like

Origin www.cnblogs.com/RiverMap/p/12569358.html