作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/projection-area-of-3d-shapes/description/

题目描述

On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.

Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).

Now we view the projection of these cubes onto the xy, yz, and zx planes.

A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.

Here, we are viewing the “shadow” when looking at the cubes from the top, the front, and the side.

Return the total area of all three projections.

Example 1:

Input: [[2]]
Output: 5

Example 2:

Input: [[1,2],[3,4]]
Output: 17
Explanation:
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.

Example 3:

Input: [[1,0],[0,2]]
Output: 8

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21

Note:

  1. 1 <= grid.length = grid[0].length <= 50
  2. 0 <= grid[i][j] <= 50

题目大意

给出了一个方阵,方阵里面的数值是柱子的高度,求三视图所有的阴影部分的面积。

解题方法

数学计算

稍微缕一下就能明白,俯视图投影就是不为0的柱子的个数,主视图、侧视图是当前视图柱子的最高值求和。

代码如下:

class Solution(object):
def projectionArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
top, front, side = 0, 0, 0
n = len(grid)
for i in range(n):
x, y = 0, 0
for j in range(n):
if grid[i][j] != 0:
top += 1
x = max(x, grid[i][j])
y = max(y, grid[j][i])
front += x
side += y
return top + front + side

也可以三视图分别进行计算,似乎更清晰明了。

class Solution:
def projectionArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
rowMax, colMax = [0] * M, [0] * N
xy = sum(0 if grid[i][j] == 0 else 1 for i in range(M) for j in range(N))
xz = sum(list(map(max, grid)))
yz = sum(list(map(max, [[grid[i][j] for i in range(M)] for j in range(N)])))
return xy + xz + yz

日期

2018 年 8 月 16 日 —— 一个月不写题,竟然啥都不会了。。加油!
2018 年 11 月 5 日 —— 打了羽毛球,有点累

【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)的更多相关文章

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

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

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

  4. 【LeetCode】892. Surface Area of 3D Shapes 解题报告(Python)

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

  5. 【Leetcode_easy】883. Projection Area of 3D Shapes

    problem 883. Projection Area of 3D Shapes 参考 1. Leetcode_easy_883. Projection Area of 3D Shapes; 完

  6. 883. Projection Area of 3D Shapes

    问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求三视图面积. Input: [[1,2],[3,4]] Output: 17 Explanation ...

  7. [LeetCode&Python] Problem 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. Each ...

  8. 【leetcode】883. Projection Area of 3D Shapes

    题目如下: 解题思路:分别求出所有立方体的个数,各行的最大值之和,各列的最大值之和.三者相加即为答案. 代码如下: class Solution(object): def projectionArea ...

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

随机推荐

  1. requests+bs4爬取豌豆荚排行榜及下载排行榜app

    爬取排行榜应用信息 爬取豌豆荚排行榜app信息 - app_detail_url - 应用详情页url - app_image_url - 应用图片url - app_name - 应用名称 - ap ...

  2. linux中对errno是EINTR的处理

    慢系统调用(slow system call):此术语适用于那些可能永远阻塞的系统调用.永远阻塞的系统调用是指调用有可能永远无法返回,多数网络支持函数都属于这一类.如:若没有客户连接到服务器上,那么服 ...

  3. KeepAlived双主模式高可用集群

    keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测:通过共用的虚拟IP地址对外提 ...

  4. CSS区分Chrome和Firefox

    CSS区分Chrome和FireFox 描述:由于Chrome和Firefox浏览器内核不同,对CSS解析有差别,因此常会有在两个浏览器中显示效果不同的问题出现,解决办法如下: /*Chrome*/ ...

  5. 日常Java 2021/10/13

    Java枚举 values(), ordinal()和valueOf()方法位于java.lang.Enum类中: values()返回枚举类中所有的值 ordinal()方法可以找到每个枚举常量的索 ...

  6. A Child's History of England.22

    CHAPTER 8 ENGLAND UNDER WILLIAM THE FIRST, THE NORMAN CONQUEROR Upon the ground where the brave Haro ...

  7. Flume(二)【入门】

    目录 一.安装部署 1.安装地址 2.安装步骤 二.入门案例 1.官方案例(nestat->logger) 2.实时监控单个追加文件(exec->hdfs) 3.实时监控目录下多个新文件( ...

  8. STL学习笔记1

    STL六大部件 容器.分配器.算法.迭代器.适配器.仿函数 他们的关系如下

  9. spring注解-自动装配

    Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值 一.@Autowired 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(Boo ...

  10. 编译安装redis之快速增加redis节点

    #: 下载安装包 [root@localhost ~]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz #:解压 [root@l ...