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. Mybatis第八篇【一级缓存、二级缓存、与ehcache整合】

    Mybatis缓存 缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题. myba ...

  2. linux中文乱码

    txt文件在linux环境下打开呈现了乱码状态. 解决方法1:在linux用iconv命令,如乱码文件名为zhongwen.txt,那么在终端输入如下命令: iconv -f gbk -t utf8 ...

  3. NIO通讯框架之Mina

          在两三年前,阿堂在技术博客(http://blog.sina.com.cn/heyitang)上曾经写过"JAVA新I/O学习系列笔记(1)"和"JAVA新I ...

  4. angularjs之ui-bootstrap的Datepicker Popup实现双日期选择控件

    最开始使用ui-bootstrap的Datepicker Popup日期选择插件实现双日期选择时间范围时,在网上搜了一些通过JS去实现的方法,不过后来发现可以不必通过JS去处理,只需要使用其自身的属性 ...

  5. 系统学习java高并发系列二

    转载请注明原创出处,谢谢! 什么是线程? 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程 ...

  6. hibernate学习手记(1)

    1. java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more ...

  7. 【充分利用你的Azure】将Azure用作云计算平台(1)

    本文将围绕几个步骤来讲. 因为本人是MSP,微软送了150刀的额度给我随便使用.这篇文章是要讲将Azure用作云计算平台,对于我来说,我是做机器学习的,那么Azure就要有机器学习的平台. 本文的目的 ...

  8. ubuntu环境配置eclipse+opencv

    blockquote { direction: ltr; color: rgb(0, 0, 0) } blockquote.western { font-family: "Liberatio ...

  9. UWP 改变Button样式

    -----some words------ 1.Control:控制 (我们理解成控件) 2.Template:模板 3.Ellipse 椭圆 4.Content 内容 5.Presenter 节目主 ...

  10. VBA 中窗体模式切换,一次设计2种表现

    Sub ModelChange() Then DoCmd.RunCommand acCmdSubformFormView ''''就这句 Me.Form.AllowEdits = True ' Mod ...