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:

  1. Input:
  2. [[0,1,1,0],
  3. [0,1,1,0],
  4. [0,0,0,1]]
  5. 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个不同方向的长度

  1. class Solution
  2. {
  3. public int longestLine(int[][] M)
  4. {
  5. if(M == null || M.length == 0)
  6. return 0;
  7.  
  8. int n = M.length;
  9. int m = M[0].length;
  10.  
  11. int[][][] arr = new int[n][m][4];
  12. int longestLine = 0;
  13.  
  14. for(int i=0; i<n; i++)
  15. {
  16. for(int j=0; j<m; j++)
  17. {
  18. if(M[i][j] == 0) // skip 0
  19. continue;
  20.  
  21. // if find a 1, put 1 into 4 cells since this number is 1
  22. for(int k=0; k<4; k++)
  23. arr[i][j][k] = 1;
  24. // then, add its 4 direction left, up-left, up, up-right value into this 4 cells
  25. if(j-1 >= 0)
  26. arr[i][j][0] += arr[i][j-1][0]; // horizontal
  27. if(j-1 >= 0 && i-1 >= 0)
  28. arr[i][j][1] += arr[i-1][j-1][1]; // diagonal
  29. if(i-1 >= 0)
  30. arr[i][j][2] += arr[i-1][j][2]; // vertical
  31. if(j+1 < m && i-1 >= 0)
  32. arr[i][j][3] += arr[i-1][j+1][3]; // anti-diagonal
  33.  
  34. int temp = Math.max(Math.max(arr[i][j][0], arr[i][j][1]), Math.max(arr[i][j][2], arr[i][j][3]));
  35. longestLine = Math.max(longestLine, temp);
  36. }
  37. }
  38.  
  39. return longestLine;
  40. }
  41. }

参考资料:

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. python之线程相关的其他方法

    一.join方法 (1)开一个主线程 from threading import Thread,currentThread import time def walk(): print('%s is r ...

  2. 01_ExtJS_HelloWorld

    1, 什么是Ext? Ext是一个Ajax框架,用于在客户端创建丰富多彩的web应用程序界面,是在Yahoo! UI的基础上发展而来的.官方网址:http://www.extjs.com/ 现在改为: ...

  3. mysql数据库-初始化sql建库建表-关联查询投影问题

    下面是一个简易商城的几张表的创建方式 drop database if exists shop ; create database shop CHARACTER SET 'utf8' COLLATE ...

  4. 来自projecteuler.net网站的练习题2

    0.题目如下: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By st ...

  5. 框架应用:Spring framework (一) - IoC技术

    IoC概念以及目标 IoC就是让原本你自己管理的对象交由容器来进行管理,其主要的目的是松耦合. IoC发展史 既然IoC的目标是为了松耦合,那它怎么做到的? 最后目标:降低对象之间的耦合度,IoC技术 ...

  6. JVM菜鸟进阶高手之路十二(jdk9、JVM方面变化, 蹭热度)

    转载请注明原创出处,谢谢! 经过 4 次跳票,历经曲折的 Java 9 正式版终于发布了!今天看着到处都是jdk9发布了,新特性说明,心想这么好的蹭热度计划能错过嘛,哈哈,所以就发了这篇文章. 目前j ...

  7. 零基础的人该怎么学习JAVA

    对于JAVA有所兴趣但又是零基础的人,该如何学习JAVA呢?对于想要学习开发技术的学子来说找到一个合适自己的培训机构是非常难的事情,在选择的过程中总是  因为这样或那样的问题让你犹豫不决,阻碍你前进的 ...

  8. Piggy Back_KEY

    Piggy Back (piggyback.pas/c/cpp) [问题描述] Bessie 和她的姐姐 Elsie 在不同的田块吃草,晚上她们都返回牛棚休息.作为聪明的奶牛,她们想设计一个方案使得步 ...

  9. Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)

    前言 前文<RESTful API实战笔记(接口设计及Java后端实现)>中介绍了RESTful中后端开发的实现,主要是接口地址修改和返回数据的格式及规范的修改,本文则简单介绍一下,RES ...

  10. oracle pl/sql 基础

    一.pl/sql developer开发工具pl/sql developer是用于开发pl/sql块的集成开发环境(ide),它是一个独立的产品,而不是oracle的一个附带品. 二.pl/sql介绍 ...