We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
3D Surface Area
3D Surface Area
Sort by
recency
|
412 Discussions
|
Please Login in order to post a comment
Python 3
Swift
int surfaceArea(vector> A) { int n = A.size(); int m = A[0].size(); int res = n * m * 2; for(int j = 0; j < m; j++){ res+= A[0][j]; res+= A[n - 1][j]; } for(int i = 0; i < n; i++){ res += A[i][0]; res += A[i][m - 1]; } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(i - 1 >= 0 && (A[i][j] - A[i - 1][j] > 0)){ res += A[i][j] - A[i - 1][j]; } if(i + 1 < n && (A[i][j] - A[i + 1][j] > 0)){ res += A[i][j] - A[i + 1][j]; } if(j - 1 >= 0 && (A[i][j] - A[i][j - 1] > 0)){ res += A[i][j] - A[i][j - 1]; } if(j + 1 < m && (A[i][j] - A[i][j + 1] > 0)){ res += A[i][j] - A[i][j + 1]; } } } return res; }
A Python 3 version that doesn't create grid A:
Code in JAVA 8. The way this work is to calculate the front, rear and corner bottom-right of each tile, then add the limits (top of first row and left of first column)