883. Projection Area of 3D Shapes
问题
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的更多相关文章
- 【Leetcode_easy】883. Projection Area of 3D Shapes
problem 883. Projection Area of 3D Shapes 参考 1. Leetcode_easy_883. Projection Area of 3D Shapes; 完
- [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 ...
- 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&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 ...
- 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【leetcode】883. Projection Area of 3D Shapes
题目如下: 解题思路:分别求出所有立方体的个数,各行的最大值之和,各列的最大值之和.三者相加即为答案. 代码如下: class Solution(object): def projectionArea ...
- [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 ...
- Leetcode883.Projection Area of 3D Shapes三维形体投影面积
在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 现在,我们查 ...
- 892. Surface Area of 3D Shapes
问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...
随机推荐
- Linux echo 命令
echo命令用于输出指定的字符串,常见用法如下: [root@localhost ~]$ echo # 输出一个空白行[root@localhost ~]$ echo "hello worl ...
- C++11新特性之五——可变参数模板
有些时候,我们定义一个函数,可能这个函数需要支持可变长参数,也就是说调用者可以传入任意个数的参数.比如C函数printf(). 我们可以这么调用. printf(); 那么这个函数是怎么实现的呢?其实 ...
- js获取一个字符串中指定字符第n次出现的位置
function nthIndexOf(str,c,n){ var x=str.indexOf(c); for(var i=0;i<num;i++){ x=str.indexOf(c,x+1); ...
- JS控制元素可见(显示)与不可见(隐藏)
方法一: document.getElementById("id").style.visibility="hidden"; document.getElemen ...
- Postgresql 创建主键并设置自动递增的三种方法
Postgresql 有以下三种方法设置主键递增的方式,下面来看下相同点和不同点. --方法一create table test_a ( id serial, name character var ...
- 170518、FastDFS_配置文件详解
http://bbs.chinaunix.net/thread-1941456-1-1.html 首先是 tracker.conf # is this config file disabled # f ...
- CentOS7.2升级默认yum安装的php版本
CentOS7.2yum安装php默认版本为5.4,可以升级通过yum安装更高版本 设置yum源 rpm -Uvh https://mirror.webtatic.com/yum/el7/webtat ...
- 《挑战程序设计竞赛》2.5 最小生成树 POJ3723 3169 1258 2377 2395 AOJ2224(1)
POJ3723 http://poj.org/problem?id=3723 题意 windy要组建一支军队,召集了N个女孩和M个男孩,每个人要付10000RMB,但是如果一个女孩和一个男孩有关系d的 ...
- ABP 样板开发框架系列
--ABP 官网与源码 http://www.aspnetboilerplate.com/ https://github.com/aspnetboilerplate --pdf和docx 文档 htt ...
- 配置oem
[oracle@kaifai ~]$ export ORACLE_SID=dbking[oracle@kaifai ~]$ export ORACLE_SID=kaifai[oracle@kaifai ...