[抄题]:

求挖掉一些区域后,能允许出现的最大十字架

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

每个有数的点先初始化为N,再逐步缩小为能扩展的最小值

[思维问题]:

知道是dfs,不知道怎么写。太需要数学技巧了,感觉就是背

[英文数据结构或算法,为什么不用别的数据结构或算法]:

新建一个数组,然后 dfs就是直接在图上操作就行

[一句话思路]:

max - min -max, 对所有点,在其四个方向中扩展的最大值中找个最小的,然后在所有点中找最大的 作为整张图的结果

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 最外层循环是一个可以重复用的变量,不只是表示从左往右扫时 行不变, 也可以表示从上往下扫时 列不变。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

for loop中的i & j 只在当前的循环中起作用,所以每次都要重复declare

[总结]:

[复杂度]:Time complexity: O(n^4) Space complexity: O(n^2)

[算法思想:递归/分治/贪心]:递归

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int orderOfLargestPlusSign(int N, int[][] mines) {
//cc
if (N == 0) return 0; //ini grids: set N, set 0
int[][] grids = new int[N][N];
for (int[] row : grids) Arrays.fill(row, N);
for (int[] mine : mines) grids[mine[0]][mine[1]] = 0; //for loop: i, 4 directions
for (int i = 0; i < N; i++) {
//l - r
for (int j = 0, l = 0; j < N; j++) {
grids[i][j] = Math.min(grids[i][j], l = (grids[i][j] == 0 ? 0 : l + 1));
} //r - l
for (int j = N -1, r = 0; j >= 0; j--) {
grids[i][j] = Math.min(grids[i][j], r = (grids[i][j] == 0 ? 0 : r + 1));
} //u - d
for (int k = 0, u = 0; k < N; k++) {
grids[k][i] = Math.min(grids[k][i], u = (grids[k][i] == 0 ? 0 : u + 1));
} //d - u
for (int k = N -1, d = 0; k >= 0; k--) {
grids[k][i] = Math.min(grids[k][i], d = (grids[k][i] == 0 ? 0 : d + 1));
}
} //for loop: for the biggest
int res = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
res = Math.max(res, grids[i][j]);
} return res;
}
}

764. Largest Plus Sign最大的dfs十字架的更多相关文章

  1. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  2. leetcode 764.Largest Plus Sign

    根据题意的话就是在非0的地方开始寻找上下左右分别能够走到的最大步长的. 那么使用暴力的方法竟然leetcode还是给过了. class Solution { public: int orderOfLa ...

  3. 764. Largest Plus Sign

    题目大意: 就是一个由1和0组成的正方形矩阵,求里面最大的加号的大小,这个大小就是长度. 什么鬼啊,本来想自己想的,结果看了半天没看懂具体什么意思,然后查了下题解,希望有人说一下意思,结果一上来就是思 ...

  4. [LeetCode] Largest Plus Sign 最大的加型符号

    In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...

  5. [Swift]LeetCode764. 最大加号标志 | Largest Plus Sign

    In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...

  6. 【leetcode】Largest Plus Sign

    题目如下: In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the giv ...

  7. PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]

    题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...

  8. 【PAT甲级】1094 The Largest Generation (25 分)(DFS)

    题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...

  9. leetcode764 Largest Plus Sign

    思路: 首先使用dp计算出在每个位置(i, j)上下左右最多有多少个连续的1,得到up[i][j], down[i][j], left[i][j], right[i][j].然后计算这四个值中的最小值 ...

随机推荐

  1. [语法]C语言中二维数组做输入参数

    C语言中二维数组做输入参数时, 可以同时指定各维长度, 可以只指定第二维的长度, 不可以只指定第一维的长度, 不可以各维长度都不指定. 一句话总结:要指定至少指定第二维,都不指定是不行的. 具体栗子如 ...

  2. linux主次设备号【转载】

    一个字符设备或者块设备都有一个主设备号和次设备号.主设备号和次设备号统称为设 备号.主设备号用来表示一个特定的驱动程序.次设备号用来表示使用该驱动程序的各 设备.例如一个嵌入式系统,有两个LED指示灯 ...

  3. JavaScript模块化-require.js,r.js和打包发布

    在JavaScript模块化和闭包和JavaScript-Module-Pattern-In-Depth这两篇文章中,提到了模块化的基本思想,但是在实际项目中模块化和项目人员的分工,组建化开发,打包发 ...

  4. C#操作mysql数据库,往mysql读取或者写入数据

    最近在开发的一个项目,需要将数据存贮在mysql数据库中,于是需要写一个操作mysql的帮助类,我采用的是官方的,还是先给出一个链接,后面有时间的话,继续更新. http://blog.csdn.ne ...

  5. new与malloc的区别,以及内存分配浅析

      从函数声明上可以看出.malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小.比如: 1 2 3 int *p; p = new int; //返回类型 ...

  6. GNU Radio: 自定义 block 实例

    综述 本文通过在GNU Radio 中编写一个block的例子,系统介绍创建一个block的过程.该 block 的功能是可以在GRC中通过滑块(WX GUI Slider)来实时改变信号源(Sign ...

  7. 引用 LPSTR、LPCSTR、LPTSTR、LPCTSTR、LPWSTR及LPCWSTR的意义及区别

    1.ANSI(即MBCS):为多字节字符集,它是不定长表示世界文字的编码方式.ANSI表示英文字母时就和ASCII一样,但表示其他文字时就需要用多字节.   2.Unicode:用两个字节表示一个字符 ...

  8. Java经典练习题_Day05

    一. 选择题 1.下列各项中的各项定义正确的是:(ACD) A.  public static void m(){}   B.  public void static m(){} C.  public ...

  9. 7503E-M-irf2配置以及bfd配置

    IRF2配置 irf domain 10 irf mac-address persistent always irf auto-update enable irf auto-merge enable ...

  10. Java垃圾回收机制和内存分配

    收集算法是内存回收的方法论,垃圾收集器是内存回收的具体实现 自动内存管理解决的是:给对象分配内存 以及 回收分配给对象的内存 为什么我们要了解学习 GC 与内存分配呢? 在 JVM 自动内存管理机制的 ...