# [caption] surface area of three-dimensional body leetcode

Time：2021-3-5

## Title: surface area of three dimensional body

On the ﹣ n * n ﹣ grid, we put some ﹣ 1 * 1 * 1 ﹣ cubes.

Each value ﹣ v = grid [i] [J] indicates that ﹣ V cubes are stacked on the corresponding cells (I, J).

Example 1:

Input: [[2]]
Output: 10
Example 2:

Input: [[1,2], [3,4]]
Output: 34
Example 3:

Input: [[1,0], [0,2]]
Output: 16
Example 4:

Input: [[1,1,1], [1,0,1], [1,1,1]]
Output: 32
Example 5:

Input: [[2,2,2], [2,1,2], [2,2,2]]
Output: 46

Tips:

1 <= N <= 50
0 <= grid[i][j] <= 50

Thinking:

[violence]

We can find the surface area of each cube and add it up.

For example: Grid [0] [1] = 3

1. He has a top area and a bottom area of top = 2
2. If grid [0] [1] is on the left side of the whole 3D body, then its left area is grid [0] [1];
3. If the height of grid [0] [2] on the right side of grid [0] [1] is smaller than that of grid [0] [1], then the area on the right side is grid [0] [1] – grid [0] [2];
4. Repeat steps 2 and 3 to check other directions.
5. Repeat the above steps, and finally get the sum of the top, bottom and side areas contributed by each position, which is the surface area of the three-dimensional body.

Code (c) above:

``````//Determine whether the position is on one side of the 3D body
bool check( int x, int y, int xlen, int ylen ) {
if( x < 0|| x >= xlen|| y < 0|| y >= ylen ) return true;
else return false;
}

int surfaceArea(int** grid, int gridSize, int* gridColSize){
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int top = 0;
int side = 0;

for( int i = 0; i < gridSize; i++ ) {
for( int j = 0; j < *gridColSize; j++ ) {
//Bottom area + top area
if( grid[i][j] != 0 ) top+=2;

for( int z = 0; z < 4; z++ ) {

int x = i + dir[z][0];
int y = j + dir[z][1];

//If it's on one side of the 3D body
if( check(x,y,gridSize,*gridColSize ) ){
side += grid[i][j];
}
else {
//If it is not on one side of the 3D object, check the height relationship between adjacent positions
if( grid[x][y] >= grid[i][j] ) continue;
else ce += grid[i][j] - grid[x][y];
}
}
}
}
return side+top;
}``````

2020-03-25-09:45:22

## Redis design and implementation 4: Dictionary Dict

In redis, the dictionary is the infrastructure. Redis database data, expiration time and hash type all take the dictionary as the underlying structure. Structure of dictionary Hashtable The implementation code of hash table is as follows:dict.h/dictht The dictionary of redis is implemented in the form of hash table. typedef struct dictht { //Hash table array, […]