How to Compute the Surface Area of 3D Shapes (Cubes Placed on Gr

  • 时间:2020-09-18 17:01:02
  • 分类:网络文摘
  • 阅读:121 次

On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j). Return the total surface area of the resulting shapes.

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

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

When we place a single cube on the grid, the surface is 6 (4×1+2), when we place a 2×1 cubes on the grid, the surface is 10 – which is 4×2+2. That is, there is only 1 top and 1 bottom, but 4 times of the number cubes that stacked together – as the connected parts (vertically) are hidden.

Then, we can iterate each verticl stacked cubes, add the top and bottom, count the side surfaces by checking the four neighbours, and add the difference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int dr[4] = {0, 1, 0, -1};
        int dc[4] = {1, 0, -1, 0};
        int N = grid.size();
        int ans = 0;
        for (int r = 0; r < N; ++ r) {
            for (int c = 0; c < N; ++ c) {
                if (grid[r][c] > 0) {
                    ans += 2;
                    for (int k = 0; k < 4; ++ k) {
                        int nr = r + dr[k];
                        int nc = c + dc[k];
                        int nv = 0;
                        if ((0 <= nr) && (0 <= nc) && 
                            (nr < N) && (nc < N)) {
                            nv = grid[nr][nc];
                        }
                        ans += max(grid[r][c] - nv, 0);
                    }
                }
            }
        }
        return ans;
    }
};
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int dr[4] = {0, 1, 0, -1};
        int dc[4] = {1, 0, -1, 0};
        int N = grid.size();
        int ans = 0;
        for (int r = 0; r < N; ++ r) {
            for (int c = 0; c < N; ++ c) {
                if (grid[r][c] > 0) {
                    ans += 2;
                    for (int k = 0; k < 4; ++ k) {
                        int nr = r + dr[k];
                        int nc = c + dc[k];
                        int nv = 0;
                        if ((0 <= nr) && (0 <= nc) && 
                            (nr < N) && (nc < N)) {
                            nv = grid[nr][nc];
                        }
                        ans += max(grid[r][c] - nv, 0);
                    }
                }
            }
        }
        return ans;
    }
};

Slightly differently, we can check the neighbours of north and west only and minus those connected surfaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int N = grid.size();
        int ans = 0;
        for (int r = 0; r < N; ++ r) {
            for (int c = 0; c < N; ++ c) {
                if (grid[r][c] > 0) {
                    ans += 4 * grid[r][c] + 2;
                    if (r > 0) ans -= min(grid[r][c], grid[r - 1][c]) * 2;
                    if (c > 0) ans -= min(grid[r][c], grid[r][c - 1]) * 2;
                }
            }
        }
        return ans;
    }
};
class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int N = grid.size();
        int ans = 0;
        for (int r = 0; r < N; ++ r) {
            for (int c = 0; c < N; ++ c) {
                if (grid[r][c] > 0) {
                    ans += 4 * grid[r][c] + 2;
                    if (r > 0) ans -= min(grid[r][c], grid[r - 1][c]) * 2;
                    if (c > 0) ans -= min(grid[r][c], grid[r][c - 1]) * 2;
                }
            }
        }
        return ans;
    }
};

Both algorithms/approaches are based on the counting, which result in O(N^2) time and O(1) space requirement.

Similar post: How to Compute the Projection Area of 3D Shapes?

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
奥数题:从这两堆煤中分别运走同样的吨数后  数学题:用4cm长的线段表示实际距离1200km  数学题:一根圆柱形木头小明的爸爸将它锯成4段  奥数题:当王明在100m赛跑冲到终点时,领先刘铭10m  数学题:小敏要买一些圣诞卡  奥数题:一列火车匀速速度向北缓缓驶去  奥数题:小胖和小丁丁骑自行车从一条公路的两端同时出发相向而行  数学题:实际的工作效率是原计划的百分之125  数学题:将圆柱体加工成体积最大的长方体  奥数题:剩下的五盒重量是原来两盒的重量 
评论列表
添加评论