← Home

892. Surface Area of 3D Shapes

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
graph traversalC++Markdown
892

This is one of those problems where the clean idea matters more than the amount of code. For 892. Surface Area of 3D Shapes, the solution in this repository is mainly a graph traversal solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: graph traversal.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are surfaceArea.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N^2), space: O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 12 ms, faster than 27.88% of C++ online submissions for Surface Area of 3D Shapes.
02//Memory Usage: 9.4 MB, less than 40.00% of C++ online submissions for Surface Area of 3D Shapes.
03
04class Solution {
05public:
06    int surfaceArea(vector<vector<int>>& grid) {
07        int m = grid.size(), n = grid[0].size();
08        int ud = 0, lat = 0;
09        for(int i = 0; i < m; i++){
10            for(int j = 0; j < n; j++){
11                if(grid[i][j]) ud += 2;
12                
13                //remember the max(xxx, 0)!
14                if(i == 0) lat += grid[i][j];
15                else lat += max(grid[i][j] - grid[i-1][j], 0);
16                
17                if(i == m - 1) lat += grid[i][j];
18                else lat += max(grid[i][j] - grid[i+1][j], 0);
19                
20                if(j == 0) lat += grid[i][j];
21                else lat += max(grid[i][j] - grid[i][j-1], 0);
22                
23                if(j == n - 1) lat += grid[i][j];
24                else lat += max(grid[i][j] - grid[i][j+1], 0);
25                
26                // cout << grid[i][j] << ", " << lat << endl;
27            }
28        }
29        
30        return ud + lat;
31    }
32};
33
34//official sol
35//Runtime: 12 ms, faster than 27.88% of C++ online submissions for Surface Area of 3D Shapes.
36//Memory Usage: 9.3 MB, less than 80.00% of C++ online submissions for Surface Area of 3D Shapes.
37//time: O(N^2), space: O(1)
38
39class Solution {
40public:
41    int surfaceArea(vector<vector<int>>& grid) {
42        vector<pair<int, int>> dirs = {{0,1},{1,0},{0,-1},{-1,0}};
43        
44        int n = grid.size();
45        int ans = 0;
46        
47        for(int r = 0; r < n; r++){
48            for(int c = 0; c < n; c++){
49                if(grid[r][c]){
50                    ans += 2;
51                    for(pair<int, int> dir : dirs){
52                        //nv : neighboring grid's value
53                        int nr = r + dir.first;
54                        int nc = c + dir.second;
55                        int nv = 0;
56                        if(nr >= 0 && nr < n && nc >= 0 && nc < n)
57                            nv = grid[nr][nc];
58                        ans += max(grid[r][c] - nv, 0);
59                    }
60                }
61            }
62        }
63        
64        return ans;
65    }
66};

Cost

Complexity

Time
O(N^2), space: O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.