【LeetCode】892. Surface Area of 3D Shapes 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/surface-area-of-3d-shapes/description/
题目描述
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
题目大意
所给出的数组是每个坐标下的z值,求整个空间图形的表面积。
解题方法
这个题乍一看和883. Projection Area of 3D Shapes非常相像。甚至我以为就是883题的答案乘以2就行。。但是我看到了第5个例子之后,眉头一皱发现事情并不简单。

实际上,要求整个图形的表面积,那么可以分解为求出每个立方体的表面积,然后减去重叠部分的面积就可以。按照这个思路,就变得简单了。
- 当只有1个立方体的时候,表面积是6;
- 如果有多个立方体摞在一起成为柱子的时候,表面积是grid[i][j] * 4 + 2;
- 如果有多个柱子的时候,需要减去重叠面积。重叠的高度是两个柱子之间,高度最小的那个的高度。因为重叠使得两个柱子都变矮了,所以要把这个高度*2.
举个例子:
对于第一个例子,输入只有一个柱子,柱子的高度是2,那么表面积是2 * 4 + 2 = 10。
再举个栗子
1,2
3,4
计算的时候是这样的:
- 首先看柱子1,表面积是6;
- 当添加柱子2,其表面积是
2 * 4 + 2 = 10,但是由于和左边的1有重叠,重叠面积是2,所以添加柱子2之后,总的表面积是6 + 10 - 2 = 14; - 当添加柱子3,柱子3的表面积是
3 * 4 + 2 = 14,由于和柱子1有重叠,重叠面积是2,所以添加柱子3之后,总面积是14 + 14 - 2 = 26; - 当添加柱子4,柱子4的表面积是
4 * 4 + 2 = 18,由于和柱子2和3有重叠,重叠面积是(2 + 3) * 2 = 10,所以添加柱子4之后,总面积是26 + 18 - 10 = 34。
Python代码如下:
class Solution(object):
def surfaceArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
area = 0
n = len(grid)
for i in range(n):
for j in range(n):
if grid[i][j]: area += grid[i][j] * 4 + 2
if i: area -= min(grid[i][j], grid[i-1][j]) * 2
if j: area -= min(grid[i][j], grid[i][j-1]) * 2
return area
二刷的写法如下。
class Solution(object):
def surfaceArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
count = 0
inner = 0
M, N = len(grid), len(grid[0])
for i in range(M):
for j in range(N):
count += grid[i][j]
if i < M - 1 and grid[i + 1][j] != 0:
inner += min(grid[i][j], grid[i + 1][j])
if j < N - 1 and grid[i][j + 1] != 0:
inner += min(grid[i][j], grid[i][j + 1])
if grid[i][j] >= 2:
inner += grid[i][j] - 1
print(count, inner)
return count * 6 - inner * 2
C++代码如下:
class Solution {
public:
int surfaceArea(vector<vector<int>>& grid) {
int res = 0;
int N = grid.size();
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
res += grid[i][j] * 6 - max(0, grid[i][j] - 1) * 2;
if (i != 0) {
res -= min(grid[i - 1][j], grid[i][j]) * 2;
}
if (j != 0) {
res -= min(grid[i][j - 1], grid[i][j]) * 2;
}
}
}
return res;
}
};
日期
2018 年 8 月 26 日 ———— 珍爱生命,远离DD!
2018 年 11 月 9 日 —— 睡眠可以
2020 年 3 月 25 日 —— 想发财
【LeetCode】892. Surface Area of 3D Shapes 解题报告(Python)的更多相关文章
- LeetCode 892 Surface Area of 3D Shapes 解题报告
题目要求 On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of ...
- [LeetCode] 892. Surface Area of 3D Shapes 三维物体的表面积
On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...
- LeetCode 883 Projection Area of 3D Shapes 解题报告
题目要求 On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. ...
- 【Leetcode_easy】892. Surface Area of 3D Shapes
problem 892. Surface Area of 3D Shapes 题意:感觉不清楚立方体是如何堆积的,所以也不清楚立方体之间是如何combine的.. Essentially, compu ...
- 892. Surface Area of 3D Shapes
问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...
- [LeetCode&Python] Problem 892. Surface Area of 3D Shapes
On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...
- 【leetcode】892. Surface Area of 3D Shapes
题目如下: 解题思路:对于v = grid[i][j],其表面积为s = 2 + v*4 .接下来只要在判断其相邻四个方向有没有放置立方体,有的话减去重合的面积即可. 代码如下: class Solu ...
- 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- C#LeetCode刷题之#892-三维形体的表面积(Surface Area of 3D Shapes)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4136 访问. 在 N * N 的网格上,我们放置一些 1 * 1 ...
随机推荐
- 自定义char类型字符,django中事务
自定义char类型字符 # 自定义char类型,继承Field父类 class MyCharField(Field): def __init__(self, max_length, *args, ** ...
- Excel-返回列表或数据库中的分类汇总(汇总可以实现要还是不要统计隐藏行功能) subtotal()
SUBTOTAL函数 函数名称:SUBTOTAL 主要功能:返回列表或数据库中的分类汇总. 使用格式:SUBTOTAL(function_num, ref1, ref2, ...) 参数说明:Func ...
- MapReduce04 框架原理Shuffle
目录 2 MapReduce工作流程 3 Shuffle机制(重点) 3.1 Shuffle机制 3.2 Partition分区 默认Partitioner分区 自定义Partitioner分区 自定 ...
- 带你全面了解 OAuth2.0
最开始接触 OAuth2.0 的时候,经常将它和 SSO单点登录搞混.后来因为工作需要,在项目中实现了一套SSO,通过对SSO的逐渐了解,也把它和OAuth2.0区分开了.所以当时自己也整理了一篇文章 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(五)-文件管理初步介绍
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
- java打jar包和运行jar包的两种方式
java打jar包和运行jar包的两种方式更详细的打包方式请参考https://www.cnblogs.com/mq0036/p/8566427.html 一.java类不依赖第三方jar包以简单的一 ...
- Linux学习 - 权限管理命令
一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod [(ugoa) ...
- Oracle带输入输出参数的存储过程
(一)使用输入参数 需求:在emp_copy中添加一条记录,empno为已有empno的最大值+1,ename不能为空且长度必须大于0,deptno为60. 创建存储过程: create or rep ...
- vueAPI (data,props,methods,watch,computed,template,render)
data Vue 实例的数据对象.Vue 将会递归将 data 的属性转换为 getter/setter,从而让 data 的属性能够响应数据变化.实例创建之后,可以通过vm.$data来访问原始数据 ...
- Project Reactor工厂方法和错误处理
工厂方法创建流 Backpressure : the ability for the consumer to signal the producer that the rate of emission ...