LeetCode-892. Surface area of three-dimensional body

On the N * N grid, we place some 1 * 1 * 1 cubes.

Each value v = grid [i] [j] means that v cubes are stacked on the corresponding cell (i, j).

Please return the surface area of ​​the final shape.

Example 1:

输入:[[2]]
输出:10

Example 2:

输入:[[1,2],[3,4]]
输出:34

Example 3:

输入:[[1,0],[0,2]]
输出:16

Example 4:

输入:[[1,1,1],[1,0,1],[1,1,1]]
输出:32

Example 5:

输入:[[2,2,2],[2,1,2],[2,2,2]]
输出:46

prompt:

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

solution:

First calculate the surface area of ​​the cube columns on each coordinate in the grid to calculate the total surface area, and then subtract the area where each cube column overlaps the right and lower cubes.

class Solution{
	 public int surfaceArea2(int[][] grid) {
        //行数
        int m=grid.length;
        //列数
        int n=grid[0].length;
        //先计算出每个网格的一列立方体的表面积
        int area=0;
        for (int i=0;i<m;i++) {
            for (int j = 0; j < n; j++) {
                if(grid[i][j]!=0){
                    int sum= grid[i][j];
                    area+=sum*4+2;
                }
            }
        }
        //从(0,0)开始,每个坐标都和右方和下方的立方体列比较,求出重合面积并减去
        //如(0,0)和(0,1)(1,0)两个位置进行比较.
        for (int i=0;i<m;i++) {
            for (int j = 0; j < n; j++) {
                //重合立方体个数
                int count=0;
                //右方和下方的坐标
                int rightY=j+1;
                int downX=i+1;
                //当前坐标的立方体列和右边立方体列重合的立方体个数
                    //判断坐标是否越界
                if(rightY<n){
                    count+=Math.min(grid[i][j],grid[i][rightY]);
                }
                //当前坐标的立方体列和下方立方体列重合的立方体个数
                if(downX<m){
                    count+=Math.min(grid[i][j],grid[downX][j]);
                }
                area=area-count*2;
            }
        }
        return area;
    }
}

Insert picture description here
Put the subtraction operation of the coincident area into the surface area of ​​the independent cube column, and integrate the following code:

class Solution{
	 public int surfaceArea(int[][] grid) {
        //行数
        int m = grid.length;
        //列数
        int n = grid[0].length;
        //先计算出每个网格的一列立方体的表面积
        int area = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] != 0) {
                    //计算当前假设为独立一列立方体列的表面积
                    int sum = grid[i][j];
                    area += sum * 4 + 2;
                    //减去与右方和下方立方体列重合的面积
                    //重合立方体个数
                    int count = 0;
                    //右方和下方的坐标
                    int rightY = j + 1;
                    int downX = i + 1;
                    //当前坐标的立方体列和右边立方体列重合的立方体个数
                    //判断坐标是否越界
                    if (rightY < n) {
                        count += Math.min(grid[i][j], grid[i][rightY]);
                    }
                    //当前坐标的立方体列和下方立方体列重合的立方体个数
                    if (downX < m) {
                        count += Math.min(grid[i][j], grid[downX][j]);
                    }
                    area = area - count * 2;

                }
            }
        }
        return area;
    }
}

Insert picture description here

Published 35 original articles · praised 7 · 40,000+ views

Guess you like

Origin blog.csdn.net/weixin_44804750/article/details/105101323