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个不同方向的长度
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)$的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)
题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [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 ...
随机推荐
- 微信小程序购物车产品计价
微信小程序购物车产品计价: 问题:当选中商品,价格累加时会出现无限循环小数 解答:在计算前先parseFloat(变量),再计算的最后使用(变量).toFixed(2)保留两位小数 例如: jiaCa ...
- “AOP代理”遇到“双上下文”
最近有小伙伴儿遇到了一个问题来咨询我,问题大致如下: 他在Service层利用Aspect设置了一个Spring AOP代理,在单元测试以及在service层代码上添加代理的时候均没有发现问题,但是在 ...
- 将Editplus添加到右键打开菜单
因为自己一直用Editplus作为文本打开工具,新的电脑将压缩文件复制了过来,但是没有右键打开了. 第一打开注册表 在命令框中输入regedit 第二在注册表中输入选项 如下图所示在下拉菜单中新建Ed ...
- 使用JavaScript实现ATM取款机
ATM机需求描述如下: 假设一个简单的ATM机的取款过程为: 首先提示用户输入密码(password),假设默认密码为111111,最多只能输入3次, 超过3次则提示用户"密码错误,请取 ...
- Java 编程思想 Chapter_14 类型信息
本章内容绕不开一个名词:RTTI(Run-time Type Identification) 运行时期的类型识别 知乎上有人推断作者是从C++中引入这个概念的,反正也无所谓,理解并能串联本章知识才是最 ...
- 一个基于Asp.net MVC的博客类网站开源了!
背景说明: 大学时毕业设计作品,一直闲置在硬盘了,倒想着不如开源出来,也许会对一些人有帮助呢,而且个人觉得这个网站做得还是不错了,毕竟是花了不少心思,希望对你有所帮助. github地址:https: ...
- pygame_polygon
今天我们要在窗口上绘制简单的多边形 1.认识几个简单的常用的颜色: black=(0,0,0) while=(255,255,255) red=(255,0,0) green=(0,255,0) bl ...
- electron入门心得
前言 前端开发桌面程序这个概念已经出现有一段时间了,这项技术也已经走向成熟,Github上nw和光electron的star就差不多有10w颗星了,github也衍生出了很多开源的桌面项目俨然成了一个 ...
- 再起航,我的学习笔记之JavaScript设计模式28(委托模式)
## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...
- localStorage和sessionStorage总结以及区别
(1)兼容的手机和浏览器: (2)使用 .setItem( key, value)存键值数据 sessionStorage.setItem("key","value&qu ...