【LeetCode】764. Largest Plus Sign 解题报告(Python)
【LeetCode】764. Largest Plus Sign 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/largest-plus-sign/description/
题目描述:
In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0. What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign. If there is none, return 0.
An “axis-aligned plus sign of 1s of order k” has some center grid[x][y] = 1 along with 4 arms of length k-1 going up, down, left, and right, and made of 1s. This is demonstrated in the diagrams below. Note that there could be 0s or 1s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1s.
Examples of Axis-Aligned Plus Signs of Order k:
Order 1:
000
010
000
Order 2:
00000
00100
01110
00100
00000
Order 3:
0000000
0001000
0001000
0111110
0001000
0001000
0000000
Example 1:
Input: N = 5, mines = [[4, 2]]
Output: 2
Explanation:
11111
11111
11111
11111
11011
In the above grid, the largest plus sign can only be order 2. One of them is marked in bold.
Example 2:
Input: N = 2, mines = []
Output: 1
Explanation:
There is no plus sign of order 2, but there is of order 1.
Example 3:
Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:
There is no plus sign, so return 0.
Note:
- N will be an integer in the range [1, 500].
- mines will have length at most 5000.
- mines[i] will be length 2 and consist of integers in the range [0, N-1].
- (Additionally, programs submitted in C, C++, or C# will be judged with a slightly smaller time limit.)
题目大意
题目要求我们找出在一个边长为N的正方形中能画出的最大的正“+”加号的边长,加号的边长就是除了从中间点到一条边的尽头的1的个数。为了加大难度,题目给出了一些值为0的坐标点,这些点上是不允许放正方形的边的。
解题方法
如果是暴力解法的话,我们很容易就写出O(n^3)的解法,就是对于每个位置,都向上下左右四个方向去寻找能拓展多远。(注意,因为方向是定死的,且四个方向长度是一致的,所以不是O(n^4))。这样肯定会超时的。
一个比较容易理解的方法就是,我们先确定一个dp数组,这个数组dp[i][j]保存的是到从i,j位置向上下左右四个方向能拓展的长度。最后每个位置能拓展多远就是上下左右四个方向能拓展长度的最小值。我选择遍历的方向是左右上下,那么到下的遍历的时候,dp数组保存的就就是最小的边长了。
这个题四个方向是对称的,因此只需要知道一个方向怎么写,那么直接改循环方向就行,根本不用思考我查找的方向到底是四个方向中的哪一个。同时使用了set把二维坐标改成了一维,可以加快查找。
时间复杂度是O(n^2),空间复杂度是O(n^2).
代码如下:
class Solution:
def orderOfLargestPlusSign(self, N, mines):
"""
:type N: int
:type mines: List[List[int]]
:rtype: int
"""
res = 0
dp = [[0 for i in range(N)] for j in range(N)]
s = set()
for mine in mines:
s.add(N * mine[0] + mine[1])
for i in range(N):
cnt = 0
for j in range(N):#left
cnt = 0 if N * i + j in s else cnt + 1
dp[i][j] = cnt
cnt = 0
for j in range(N - 1, -1, -1):#right
cnt = 0 if N * i + j in s else cnt + 1
dp[i][j] = min(dp[i][j], cnt)
for j in range(N):
cnt = 0
for i in range(N):#up
cnt = 0 if N * i + j in s else cnt + 1
dp[i][j] = min(dp[i][j], cnt)
cnt = 0
for i in range(N - 1, -1, -1):#down
cnt = 0 if N * i + j in s else cnt + 1
dp[i][j] = min(dp[i][j], cnt)
res = max(dp[i][j], res)
return res
如果用四个变量代表上下左右方向的话可以缩短一下代码:
class Solution:
def orderOfLargestPlusSign(self, N, mines):
"""
:type N: int
:type mines: List[List[int]]
:rtype: int
"""
res = 0
dp = [[N for i in range(N)] for j in range(N)]
s = set()
for mine in mines:
dp[mine[0]][mine[1]] = 0
for i in range(N):
l, r, u, d = 0, 0, 0, 0
for j in range(N):
l = l + 1 if dp[i][j] else 0
r = r + 1 if dp[j][i] else 0
u = u + 1 if dp[i][N - 1 -j] else 0
d = d + 1 if dp[N - 1 - j][i] else 0
dp[i][j] = min(dp[i][j], l)
dp[j][i] = min(dp[j][i], r)
dp[i][N - 1 - j] = min(dp[i][N - 1 - j], u)
dp[N - 1 - j][i] = min(dp[N - 1 - j][i], d)
for i in range(N):
for j in range(N):
res = max(res, dp[i][j])
return res
参考资料:
http://www.cnblogs.com/grandyang/p/8679286.html
日期
2018 年 9 月 16 日 ———— 天朗气清,惠风和畅
【LeetCode】764. Largest Plus Sign 解题报告(Python)的更多相关文章
- LeetCode 812 Largest Triangle Area 解题报告
题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- leetcode 764.Largest Plus Sign
根据题意的话就是在非0的地方开始寻找上下左右分别能够走到的最大步长的. 那么使用暴力的方法竟然leetcode还是给过了. class Solution { public: int orderOfLa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- 论文翻译:2020_Weighted speech distortion losses for neural-network-based real-time speech enhancement
论文地址:基于神经网络的实时语音增强的加权语音失真损失 论文代码:https://github.com/GuillaumeVW/NSNet 引用:Xia Y, Braun S, Reddy C K A ...
- 日常Java 2021/10/17
今天开始Javaweb编译环境调试,从tomcat容器开始,然后mysql的下载,连接工具datagrip,navicat for mysql,然后就是编写自己的sql,安装jdbc,eclipse连 ...
- 容器之分类与各种测试(四)——set
set和multiset的去别在于前者的key值不可以重复,所以用随机数作为其元素进行插入时,遇到重复元素就会被拒绝插入(但是程序不会崩溃). 例程 #include<stdexcept> ...
- Oracle中建表及表操作
一.创建表 Oracle中的建表语句:create table 表名( 字段名1 数据类型 列属性,字段名2 数据类型 列属性,...... ) 如:创建表OA_DM.DM_GY_USER https ...
- java中的原子操作类AtomicInteger及其实现原理
/** * 一,AtomicInteger 是如何实现原子操作的呢? * * 我们先来看一下getAndIncrement的源代码: * public final int getAndIncremen ...
- spring boot @EnableWebMvc禁用springMvc自动配置原理。
说明: 在spring boot中如果定义了自己的java配置文件,并且在文件上使用了@EnableWebMvc 注解,那么sprig boot 的默认配置就会失效.如默认的静态文件配置路径:&quo ...
- HDC2021技术分论坛:进程崩溃/应用卡死,故障频频怎么办?
作者:jiwenqiang,DFX技术专家 提到开发一个产品,我们通常首先想到的是要实现什么样的功能,但是除了功能之外,非功能属性也会很大程度上影响一个产品的体验效果,比如不定时出现的应用卡死.崩溃 ...
- numpy基础教程--where函数的使用
在numpy中,where函数是一个三元运算符,函数原型为where(condition, x, y),意思是当条件成立的时候,将矩阵的值设置为x,否则设置为y 一个很简单的应用就是,在一个矩阵当中, ...
- Nginx配置重定向
目录 一.简介 二.配置 访问a页面重定向到b页面 访问当前nginx,重定向到其他网址 一.简介 据相关变量重定向和选择不同的配置,从一个 location 跳转到另一个 location,不过这样 ...
- MySQL信息系统函数