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. MySql数据库第一天

    LAMP  linux apache mysql php的关系服务器软件 “服务器” 数据库 操作 mysql常用数据类型:int varchar float double bit datetime. ...

  2. Hyperledger Fabric 1.0 从零开始(七)——启动Fabric多节点集群

    5:启动Fabric多节点集群 5.1.启动orderer节点服务 上述操作完成后,此时各节点的compose配置文件及证书验证目录都已经准备完成,可以开始尝试启动多机Fabric集群. 首先启动or ...

  3. csv文件读取

    from urllib.request import urlopen from io import StringIO import csv data = urlopen("http://py ...

  4. ExtJS4为form表单必填项添加红色*标识

    通常情况下,ExtJS4的form表单必填项在输入状态下会有特殊提示,非输入状态下,显示却和其他项没有任何区别.为使必填项更加容易区分,我们需要根据allowBlank的属性值,为form表单中的必填 ...

  5. 深入理解计算机系统(2.5)------C语言中的有符号数和无符号数以及扩展和截断数字

    上一篇博客我们讲解了计算机中整数的表示,包括无符号编码和补码编码,以及它们之间的互相转换,个人觉得那是非常重要的知识要点.这篇博客我们将介绍C语言中的有符号数和无符号数以及扩展和截断数字. 1.C语言 ...

  6. MySQL线程池的引入可以提高我们的MySQL的性能

    支持线程池的版本:MySQL 企业版本,MySQL percona的分支 MariDB 的版本.我们知道我们的MySQL 语句是不支持硬解析的,没有无SQL 解析 cache.每个连接对应一个线程,我 ...

  7. Android 之http编程

    HTTP-GET和HTTP-POST定义: 都是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语义. 每个HTTP-GET和HTTP-POST都由一系列HTTP请求 ...

  8. JVM菜鸟进阶高手之路一(一次与笨神,阿飞近距离接触修改JVM)

    转载请注明原创出处,谢谢! 今天在JVMPocket群里面看见,阿牛发了一个gc截图,之后ak47截图了特别恐怖,我就觉得好奇,去看看服务情况,截图日志如下 关于jstat命令详情可以参考:https ...

  9. Ubuntu16.04.1上搭建分布式的Redis集群

    为什么要集群: 通常为了,提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis是一个很好的Cache工具,当然了还有Memcached,这里只讲Redis.在我们的电 ...

  10. webpack2系列step1

    第一篇:HTML 本文将一步一步的介绍webpack2的配置,从最基础的一直到与node结合. 操作都一样: midir step1 && cd step1 npm init -y n ...