LWC 67: 764. Largest Plus Sign

传送门:764. Largest Plus Sign

Problem:

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.)

思路:
动态规划,分别记录4个方向上的最大连续1的个数。比如”1001111”, 每个位置出现的最大连续1的个数分别为:”1001234”,有了4个方向的最长连续1,order就是这四个方向的最小值,遍历每个位置的order,求出最大order即可。

Java版本:

    public int orderOfLargestPlusSign(int N, int[][] mines) {int[][][] grid = new int[N][N][4];for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {for (int k = 0; k < 4; ++k) {grid[i][j][k] = 1;}}}for (int[] mine : mines) {int r = mine[0];int c = mine[1];for (int k = 0; k < 4; ++k) grid[r][c][k] = 0;}for (int i = 0; i < N; ++i) {for (int j = 1; j < N; ++j) {if (grid[i][j][0] == 1)grid[i][j][0] += grid[i][j - 1][0]; }for (int j = N - 2; j >= 0; --j) {if (grid[i][j][1] == 1)grid[i][j][1] += grid[i][j + 1][1];}}for (int j = 0; j < N; ++j) {for (int i = 1; i < N; ++i) {if (grid[i][j][2] == 1)grid[i][j][2] += grid[i - 1][j][2];}for (int i = N - 2; i >= 0; --i) {if (grid[i][j][3] == 1)grid[i][j][3] += grid[i + 1][j][3];}}int ans = 0;for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {int order = Math.min(Math.min(grid[i][j][0], grid[i][j][1]), Math.min(grid[i][j][2], grid[i][j][3]));ans = Math.max(ans, order);}}return ans;}

Python版本:

class Solution(object):def orderOfLargestPlusSign(self, N, mines):""":type N: int:type mines: List[List[int]]:rtype: int"""grid = [[1] * N for _ in range(N)]lf = [[0] * N for _ in range(N)]dn = [[0] * N for _ in range(N)]rt = [[0] * N for _ in range(N)]up = [[0] * N for _ in range(N)]for i, j in mines: grid[i][j] = 0for i in xrange(N):for j in xrange(N):if grid[i][j] == 1:lf[i][j] = 1 if j == 0 else lf[i][j - 1] + 1if grid[j][i] == 1:dn[j][i] = 1 if j == 0 else dn[j - 1][i] + 1for i in xrange(N):for j in xrange(N - 1, -1, -1):if grid[i][j] == 1:rt[i][j] = 1 if j == N - 1 else rt[i][j + 1] + 1if grid[j][i] == 1:up[j][i] = 1 if j == N - 1 else up[j + 1][i] + 1ans = 0for i in xrange(N):for j in xrange(N):order = min(lf[i][j], rt[i][j], up[i][j], dn[i][j])ans = max(ans, order)return ans

LWC 67: 764. Largest Plus Sign相关推荐

  1. Leetcode 764. Largest Plus Sign

    思路:动态规划.对于第i行第j列的元素grid[i][j]表示的是这个元素的plus sign的等级,初始化不在mines中的元素对应的grid值为较大值(只要大于N/2即可),在mines中的元素对 ...

  2. leetcode 764. Largest Plus Sign | 764. 最大加号标志(Java)

    题目 https://leetcode.com/problems/largest-plus-sign/ 题解 class Solution {public int orderOfLargestPlus ...

  3. leetcode 764.Largest Plus Sign

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

  4. leetcode算法练习 JavaScript实现

    leetcode 表格内容由spider.js从leetcode-cn.com爬取. 已做题目答案也从leetcode-cn.com中爬取并生成文件. 解题进度:已解决 140/637 - 简单 94 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  6. C++流操纵算子(格式控制)

    输入/输出的数据没有指定格式,它们都按缺省的格式输入/输出.然而,有时需要对数据格式进行控制.这时需利用ios类中定义的格式控制成员函数,通过调用它们来完成格式的设置. ios类的格式控制函数如下所示 ...

  7. 【读书笔记】数据出现多重共线性情况:岭回归,lasso回归,适应性lasso回归,偏最小二乘回归

    # 2.2.3 数据出现多重共线性情况:岭回归,lasso回归,适应性lasso回归,偏最小二乘回归 # 有一些关于多重共线性的度量,其中之一是容忍度(tolerance)或(等价的)方差膨胀因子(v ...

  8. 【LeetCode 剑指offer刷题】字符串题6:67 把字符串转成整数

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 67 把字符串转成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符 ...

  9. 【APUE】Chapter17 Advanced IPC sign extension 结构体内存对齐

    17.1 Introduction 这一章主要讲了UNIX Domain Sockets这样的进程间通讯方式,并列举了具体的几个例子. 17.2 UNIX Domain Sockets 这是一种特殊s ...

  10. 【LeetCode】剑指 Offer 67. 把字符串转换成整数

    [LeetCode]剑指 Offer 67. 把字符串转换成整数 文章目录 [LeetCode]剑指 Offer 67. 把字符串转换成整数 package offer;public class So ...

最新文章

  1. mybatis中like模糊查询的几种写法及注意点
  2. 三个大数据处理框架:Storm,Spark和Samza 介绍比较
  3. 在IDEA 中用maven创建web项目
  4. 为sap的alv的最左侧添加【选中】按钮用于同时选中多行...
  5. 371. 两整数之和
  6. xp 设备管理器 android,XP设备管理器怎么打开?
  7. 复杂电网三相短路计算的matlab仿真,复杂电网三相短路计算的MATLAB仿真电力系统分析课设报告 - 图文...
  8. ural(Timus) 1019 Line Painting
  9. java自动封箱_Java程序员面试,自动封箱/拆箱原理与包装类的缓冲机制你知道么?(转)...
  10. python灰度处理_python 简单图像处理(9) 灰度变换
  11. 通过脚本下派WsusAgent3.0.exe
  12. 百度人脸识别使用总结(环境+测试+人脸库管理)
  13. 【kvm虚拟化详解01】-虚拟化概述和主流方案介绍
  14. Fiji-imageJ 无法打开
  15. 【ubutun22.04】mac修改与吉林大学校园网链接
  16. Pr 入门教程:如何向影片中的剪辑添加过渡效果?
  17. 大数据技术应用需要注意什么?
  18. 根据指定日期获取该日期所在周的所有日期
  19. 中国SaaS驶入「2.0时代」
  20. Xubuntu系统要求

热门文章

  1. SpringBoot整合redis,启动提示错误信息: An exception was thrown by org.redisson.misc.RedissonPromise$$Lambda$682
  2. 【无标题】学习浩辰CAD软件的心得
  3. url data 模式(url scheme data)
  4. Rust权威指南 读书笔记
  5. 迁移学习论文阅读感想(初步)
  6. 网易微专业python数据分析_网易微专业_Python数据分析师 01 数据思维导论:如何从数据中挖掘价值?...
  7. 百度地图添加多个大头针自定义图片
  8. linux那些事儿之我是i2c -- Gsenser(三)
  9. IPETRONIK为您提供专业化的数据采集软件IPEmotion进行声学采集与分析
  10. RC电路的充放过程C语言实现,一文讲解RC电路耦合、相移、滤波、微分、积分......