• LeetCode 562. 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 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个不同方向的长度

     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

  • 相关阅读:
    log4net preserveLogFileNameExtension 和 watch
    BootStrap自带的图标
    git fetch批处理,遍历一个文件夹下的所有子目录,执行git fetch --all
    Recommended Settings for Tracing and Message Logging
    蜗牛—JSONJ学习ava转变
    Do you master on array in C ?
    全面推行使用智能指针的权利
    2014/08/23——OJ出现waiting...
    在Windows通过使用MinGW静态编译Assimp
    Ubuntu12.04password正确 入口的桌面(测试的恢复正常)
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7633684.html
Copyright © 2020-2023  润新知