【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/ 题目地址 ...
随机推荐
- mysql-彻底删除方法
一.如果是使用yum安装的mysql,使用如下命令进行卸载(不能确定使用何种方式安装的mysql情况下,按后续步骤一一进行处理即可): yum remove mysql mysql-server my ...
- Excel-vlookup(查找值,区域范围,列序号,0)如何固定住列序列号,这样即使区域范围变动也不受影响
突然,发现VLOOKUP的列序列号并不会随着区域范围的改变而自动调节改变,只是傻瓜的一个数,导致V错值.所有,就想实现随表格自动变化的列序号. 方法一:在列序号那里,用函数得出永远想要的那个列在区域范 ...
- 『与善仁』Appium基础 — 16、APPium基础操作API
目录 1.前置代码 2.安装和卸载APP 3.判断APP是否已安装 4.关闭APP软件和关闭驱动对象 5.发送文件到手机和获取手机中的文件 6.获取当前屏幕内元素结构(重点) 7.脚本内启动其他APP ...
- 1005.K次取反后最大化的数组和
1005.K次取反后最大化的数组和 目录 1005.K次取反后最大化的数组和 题目 题解 排序+维护最小值min 题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 ...
- 逻辑学与Prolog学习笔记
int a = 3 + 5; 很自然.如果Matrix a, b要加呢?没有运算符重载,a + b是不行的,只能add(a, b). int a = add(3, 5)也行.如果函数名可以用+呢?+( ...
- AI ubantu 环境安装
ubantu安装记录 apt install python3-pip anaconda安装 https://repo.anaconda.com/archive/Anaconda3-2020.11-Li ...
- get请求url参数中有+、空格、=、%、&、#等特殊符号的问题解决
url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好?解决办法将这些字符转化成服务器可以识别的字符,对应关系如下:URL字符转义 用其它 ...
- 2.使用Lucene开发自己的搜索引擎–indexer索引程序中基本类介绍
(1)Directory:Directory类描述了Lucene索引的存放位置,它是一个抽象,其子类负责具体制定索引的存储路径.FSDirectory.open方法来获取真实文件在文件系统中的存储路径 ...
- layui-入门
1.下载layui 2.导入layui <title>开始使用layui</title> <!-- 导入js样式 --> <link rel="st ...
- 【HarmonyOS】【DevEco Studio】NOTE04:How to Jump to a Page(页面间的跳转)
页面创建与基本设置 创建页面 创建两个新页面,分别为AbilityPage1.AbilityPage2 设置页面基本内容 以AbilityPage1为例 导包 import com.example.m ...