作者: 负雪明烛
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. 1 <= N <= 50
  2. 0 <= grid[i][j] <= 50

题目大意

所给出的数组是每个坐标下的z值,求整个空间图形的表面积。

解题方法

这个题乍一看和883. Projection Area of 3D Shapes非常相像。甚至我以为就是883题的答案乘以2就行。。但是我看到了第5个例子之后,眉头一皱发现事情并不简单。

实际上,要求整个图形的表面积,那么可以分解为求出每个立方体的表面积,然后减去重叠部分的面积就可以。按照这个思路,就变得简单了。

  1. 当只有1个立方体的时候,表面积是6;
  2. 如果有多个立方体摞在一起成为柱子的时候,表面积是grid[i][j] * 4 + 2;
  3. 如果有多个柱子的时候,需要减去重叠面积。重叠的高度是两个柱子之间,高度最小的那个的高度。因为重叠使得两个柱子都变矮了,所以要把这个高度*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)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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. ...

  4. 【Leetcode_easy】892. Surface Area of 3D Shapes

    problem 892. Surface Area of 3D Shapes 题意:感觉不清楚立方体是如何堆积的,所以也不清楚立方体之间是如何combine的.. Essentially, compu ...

  5. 892. Surface Area of 3D Shapes

    问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...

  6. [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 ...

  7. 【leetcode】892. Surface Area of 3D Shapes

    题目如下: 解题思路:对于v = grid[i][j],其表面积为s = 2 + v*4 .接下来只要在判断其相邻四个方向有没有放置立方体,有的话减去重合的面积即可. 代码如下: class Solu ...

  8. 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  9. C#LeetCode刷题之#892-三维形体的表面积(Surface Area of 3D Shapes)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4136 访问. 在 N * N 的网格上,我们放置一些 1 * 1 ...

随机推荐

  1. applogs流量数据项目学习

    一. 项目介绍 项目的功能主要是面向App开发商提供App使用情况的统计服务 主要是基于用户启动app的统计分析,app只要启动就会上报一条日志记录 (启动日志),当然也会有其他的日志比如说页面访问日 ...

  2. Use of explicit keyword in C++

    Predict the output of following C++ program. 1 #include <iostream> 2 3 using namespace std; 4 ...

  3. OC-引用计数器,内存管理,野指针

    总结 全局断点 →-->+-->Add Exception Breakpoint 开启僵尸监听 打开Edit scheme -->Diagnostics-->Enable Zo ...

  4. Data Calendar

    1.Date对象 Date类在java.util包中.使用Date类的无参数构造方法创建的对象可以获取本地当前时间. 用Date的构造方法Date(long time)创建的Date对象表 示相对19 ...

  5. delete() and free() in C++

    In C++, delete operator should only be used either for the pointers pointing to the memory allocated ...

  6. 【Java 基础】Java动态代理

    Java动态代理InvocationHandler和Proxy java动态代理机制中有两个重要的类和接口InvocationHandler(接口)和Proxy(类),这一个类Proxy和接口Invo ...

  7. 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式

    一.普通请求方法发送 PUT 请求 1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下: <form action="/emps&quo ...

  8. 【力扣】973. 最接近原点的 K 个点

    我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. (这里,平面上两点之间的距离是欧几里德距离.) 你可以按任何顺序返回答案.除了点坐标的顺序之外, ...

  9. 【MySQL】亲测可用的教程筛选:安装与卸载

    windows版本的 安装看这篇,非常详细:https://www.cnblogs.com/winton-nfs/p/11524007.html 彻底清除:https://www.pianshen.c ...

  10. Apache APISIX 的安装和配置请求转发url匹配

    安装apisix套件 创建一个apisix文件夹,在apisix文件夹下再创建一个etcd_data文件夹,用来持久化etcd的数据 在apisix文件夹下 新建3个文件 config.yaml,  ...