问题

NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求三视图面积。

Input: [[1,2],[3,4]]

Output: 17

Explanation: 见下图

思路

对于俯视图,只要一个格子有值,面积值就加1。

对于正视图(面朝x轴),对于某一个x,在y轴方向上拥有的最高grid值,表示,该x顺着y轴看过去看到的面积值。

对于侧视图(面朝y轴),对于某一个y,在x轴方向上拥有的最高grid值,表示,该y顺着y轴看过去看到的面积值。

把这些面积值加起来即可。

时间复杂度O(n^2,空间复杂度O(1)

代码

class Solution(object):
def projectionArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
s = 0
n = len(grid)
for i in range(n):
best_row = 0
best_col = 0
for j in range(n):
if(grid[i][j] > 0):
s += 1
best_row = max(best_row, grid[i][j])
best_col = max(best_col, grid[j][i])
s += best_row + best_col
return s

类似题目

892. Surface Area of 3D Shapes

883. Projection Area of 3D Shapes的更多相关文章

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

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

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

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

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

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

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

  7. [Swift]LeetCode883. 三维形体投影面积 | 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. Leetcode883.Projection Area of 3D Shapes三维形体投影面积

    在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 现在,我们查 ...

  9. 892. Surface Area of 3D Shapes

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

随机推荐

  1. EditPlus详解

    如何让EditPlus支持LUA教程是本文要介绍的内容,这次主要介绍一下学习Lua之前的准备工作.关于在EditPlus中实现lua的安装,具体内容来看本文详解. (1) 下载Lua安装包,最新版本是 ...

  2. 当新增页面和编辑页面使用同一jsp时

    <c:if test="${type eq '1'}"><title>新增页面</title></c:if> <c:if te ...

  3. Maven 环境变量设置

    下载Maven 官方下载地址:http://maven.apache.org/download.html 选择你所希望下载的版本,并保存到常用安装目录.这里以Maven 3.2.2 (Binary z ...

  4. MQTT协议笔记之订阅

    前言 记忆不太好的时候,只能翻看以前的文章/笔记重新温习一遍,但找不到MQTT协议有关订阅部分的描述,好不容易从Evernote中找到贴出来,这样整个MQTT协议笔记,就比较齐全了. SUBSCRIB ...

  5. 布局的诡异bug合集+解决方法(更新中)

    1.元素内部子元素的margin的边界线基准点的问题 论如何生硬起名字!!我反正已经被自己总结的题目绕晕了... “演员”介绍: 外层父元素:蓝色边框: 内部子元素:绿色区域: 粉红色区域是元素内部绿 ...

  6. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  7. LeetCode——Happy Number

    Description: Write an algorithm to determine if a number is "happy". A happy number is a n ...

  8. ELK平台介绍

    在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自:http://baidu.blog.51cto.com/71938/1676798 日志主要包括系统日志.应用程序日志和安全 ...

  9. git下载和上传项目

    首先是git的下载和安装: https://www.cnblogs.com/chenxqNo01/p/6372933.html git的简单使用: 从码云 clone 项目: git clone ht ...

  10. Web安全开发建议

    版权声明:原创作品,如需转载,请与作者联系.否则将追究法律责任. Web安全问题,很多时候会被人所忽略,安全漏洞造成了很多不必要的维护和开发任务,产生的问题有时候更是致命的. 实际上,只要我们养成一些 ...