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 思路 只要 ...
随机推荐
- java集合的中的集合关系实现或继承关系图
放在这儿一目了然.
- 最简单的VS-Qt-CMake项目框架
使用qtcreator新建一个空工程,可以得到main.cpp,mainwindow.cpp,mainwindow.h和mainwindow.ui四个文件 下面主要介绍CMakeLists.txt的内 ...
- UISearchBar和UISearchDisplayController
原文 http://hi.baidu.com/happywilma0118/item/e6d5730a499bba1b3a53eef8 UISearchBar继承自UIView.UIResponder ...
- MyBatis——Java API
Java API 既然你已经知道如何配置 MyBatis 和创建映射文件,你就已经准备好来提升技能了. MyBatis 的 Java API 就是你收获你所做的努力的地方.正如你即将看到的,和 JDB ...
- Android ImageResizer:inSampleSize
import android.annotation.TargetApi; import android.content.Context; import android.content.res.Reso ...
- Python的函数名作为参数传入调用以及map、reduce、filter
零.python的lambda函数: #lambda function func = lambda x : x+1 #这里是一个匿名函数,x是参数,x+1是对参数的操作 func(1)= 2 多个参数 ...
- java合并两个升序数组为一个新的有序数组
转自:http://blog.csdn.net/laozhaokun/article/details/37531247 题目:有两个有序数组a,b,现需要将其合并成一个新的有序数组. 简单的思路就是先 ...
- IntelliJ Idea 2018 注册码
转自:http://idea.lanyus.com/ *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的IntelliJ IDEA授权服务 ...
- 再谈js的作用域
再谈js的作用域 面试中遇到的题目: 题目一: var word = "hello world"; (function(){ alert(word); var word = ...
- 查看、关闭登录到linux的终端
基本概念: tty(终端设备的统称):tty一词源于Teletypes,原来指的是电传打字机,是通过串行线用打印机键盘阅读和发送信息的东西,后来这东西被键盘和显示器取代,所以现在叫终端比较合适.终端是 ...