Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.

Example:

Input:
[[0,1,1,0],
[0,1,1,0],
[0,0,0,1]]
Output: 3

Hint: The number of elements in the given matrix will not exceed 10,000.


题目标签:Array

  这道题目给了我们一个2d array,让我们找出最长1的长度。这里可以有四个方向,横向,纵向,还有两个不同方向的 斜线。

  一开始想到的是维护一个dp 2d array,如果遍历2d array的话,对于rows 是从上到下,对于 每一个row 是从左到右。所以每一个cell的累加方向只能有4个可能:

  1. 从左边的cell 累加;

  2. 从左上方的cell 累加;

  3. 从上方的cell 累加;

  4. 从右上方的cell 累加。

  但是这里存在一个问题,因为有4个方向的累加,在之后的累加里,会搞不清楚之前存在的值,是从哪个方向累加来的。所以对于每一个cell,我们要把4个方向分开存放。

  这样的话,我们可以设一个dp 3d array 在最后一个维度的array里,存放4种不同方向的累加。

  比如:最后一个维度 array [1, 2, 3, 4] 每一个value 都代表着对应方向的累加。[左边,左上,上方,右上]。

  有了这样的结构,在遍历中,我们就可以累加4个方向的长度,每次遇到M 里是1的cell, 就把 3d array里的4个方向值都设为1,再检查这个cell 的 4个方向,左边,左上,上方,右上,进行累加。在累加完毕后,更新最大的长度。

Java Solution:

Runtime beats 66.48%

完成日期:10/05/2017

关键词:Array, Dynamic Programming

关键点:维护一个3d array,累加4个不同方向的长度

 class Solution
{
public int longestLine(int[][] M)
{
if(M == null || M.length == 0)
return 0; int n = M.length;
int m = M[0].length; int[][][] arr = new int[n][m][4];
int longestLine = 0; for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(M[i][j] == 0) // skip 0
continue; // if find a 1, put 1 into 4 cells since this number is 1
for(int k=0; k<4; k++)
arr[i][j][k] = 1;
// then, add its 4 direction left, up-left, up, up-right value into this 4 cells
if(j-1 >= 0)
arr[i][j][0] += arr[i][j-1][0]; // horizontal
if(j-1 >= 0 && i-1 >= 0)
arr[i][j][1] += arr[i-1][j-1][1]; // diagonal
if(i-1 >= 0)
arr[i][j][2] += arr[i-1][j][2]; // vertical
if(j+1 < m && i-1 >= 0)
arr[i][j][3] += arr[i-1][j+1][3]; // anti-diagonal int temp = Math.max(Math.max(arr[i][j][0], arr[i][j][1]), Math.max(arr[i][j][2], arr[i][j][3]));
longestLine = Math.max(longestLine, temp);
}
} return longestLine;
}
}

参考资料:

https://discuss.leetcode.com/topic/87197/java-o-nm-time-dp-solution

LeetCode 题目列表 - LeetCode Questions List

LeetCode 562. Longest Line of Consecutive One in Matrix(在矩阵中最长的连续1)$的更多相关文章

  1. LC 562. Longest Line of Consecutive One in Matrix

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  2. [LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  3. Longest Line of Consecutive One in Matrix

    Given a 01 matrix, find the longest line of consecutive 1 in the matrix. The line could be horizonta ...

  4. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  5. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  6. 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)

    题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...

  7. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. leetcode 128. Longest Consecutive Sequence ----- java

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

随机推荐

  1. PowerBI开发 第五篇:关系的设计

    PowerBI 使用 内存的列式数据库 VertiPaq,用于对已发布的数据集进行数据压缩和快速处理,能够使PowerBI报表执行脱机访问,面向列的处理,高度优化对1:N关系的处理性能.PowerBI ...

  2. Java I/O 从0到1 - 第Ⅰ滴血 File

    前言 File 类的介绍主要会依据<Java 编程思想>以及官网API .相信大家在日常工作中,肯定会遇到文件流的读取等操作,但是在搜索过程中,并没有找到一个介绍的很简洁明了的文章.因此, ...

  3. 搭建连接MySql的三层架构的ASP.NetCore2.0的WebApi

    里我们用三层架构搭建一个连接MySql的ASP.netCore模板的WebApi项目 首先添加WebApi项目(ASP.NetCore版本) 右键解决方案>新建项目> 选择Web>A ...

  4. Babel初体验

    原文地址:→传送门 写在前面 现在es6很流行,尽管各大浏览器都还不能支持它的新特性,但是小伙伴们还是很中意它呀,于是小小的学习的一下 Babel 这里我们不介绍es6相关内容,只是说下入坑前奏,记录 ...

  5. Jquery第三篇【AJAX 相关的API】

    前言 前面我们已经学了讲解了Jquery的选择器,关于DOM 的API还有事件的API.本博文需要讲解Jquery对AJAX的支持- 我们在开始使用JavaScript学习AJAX的时候,创建异步对象 ...

  6. Spring-Hibernate-web的延迟加载方案

    1,现象与问题 /** * 由于Hibernate存在延迟加载问题,当Dao事务提交之后,session就关闭: * 此时如果到显示层就没有办法获取对象,使用openSessionInViewer是解 ...

  7. 关于APP分享到QQ、微信等

    <script> var shares=null;        var Intent=null,File=null,Uri=null,main=null; function plusRe ...

  8. python 实现登录程序

    本文介绍一个用python 实现的登录程序.python新手们可以参考一下. 用户信息存放于一个文件中,需要引入文件,校验输入的用户名.密码是否跟用户列表中的用户名密码相匹配,如果匹配,这登录成功,否 ...

  9. Qt--自定义View

    这是上一篇文章的续篇,关于自定义View. 多个View内部可以映射到同一个数据模型,也可以映射不同的数据结构:可以使用所有数据,也可以只使用部分数据.因为视图层与数据层的分离,操作相对比较灵活. 1 ...

  10. 【个人笔记】《知了堂》前端mySql基础

    指定列之后添加: ALTER TABLE 表名 ADD 添加的新列名 INT AFTER 指定列之后 第一个位置: ALTER TABLE 表名 ADD 添加的新列名 varchar(20) AFTE ...