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 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.
Median的题就是简单一点。
1. DFS没有问题。
2. 怎么保存已经遍历过的点,因为每一个点可能有4个方向,所以可以开一个map记录每一个点的四个方向有没有被经过。
我的思路,Runtime有点慢。
Runtime: 88 ms, faster than 13.53% of C++ online submissions for Longest Line of Consecutive One in Matrix.
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
private:
unordered_map<int, vector<bool>> mp;
int dirs[][] = {{,},{,},{,},{,-}};
int n,m;
public:
int longestLine(vector<vector<int>>& M) {
n = M.size();
m = M[].size();
int ret;
REP(i,n){
REP(j,m){
mp[i*m+j] = {false,false,false,false};
}
}
REP(i,n){
REP(j,m){
if(M[i][j] == ){
dfs(M, i, j, ret);
}
}
}
return ret;
}
bool valid(int x, int y){
return x < n && x >= && y < m && y >= ;
} void dfs(vector<vector<int>>& M, int x, int y, int& ret){
if(x < || y < || x >= n || y >= m) return ;
if(M[x][y] == ) return ;
REP(i, ){
if(mp[x*m+y][i]) continue;
mp[x*m+y][i] = true;
int tmpx = x, tmpy = y, dx = dirs[i][], dy = dirs[i][];
int cnt = ;
while(valid(tmpx+dx,tmpy+dy) && M[tmpx+dx][tmpy+dy] == ){
tmpx += dx;tmpy += dy;
mp[tmpx*m+tmpy][i] = true;
cnt++;
}
tmpx = x, tmpy = y, dx = -dirs[i][], dy = -dirs[i][];
while(valid(tmpx+dx,tmpy+dy) && M[tmpx+dx][tmpy+dy] == ){
tmpx += dx; tmpy += dy;
mp[tmpx*m+tmpy][i] = true;
cnt++;
}
ret = max(ret, cnt);
}
}
};
//[[0,1,1,0],
//[0,1,1,0],
//[0,0,0,1]] int main(){
vector<vector<int>> M {{,,,},{,,,},{,,,}};
return ;
}
另一个思路使用dp,dp的精髓在于前后的状态无关,有点类似马尔可夫链。
从矩阵第一行向下遍历,每一次保存的就是当前点四个方向的最大长度,如果点是1,该方向加1。也是很好的思路。
class Solution {
public:
int longestLine(vector<vector<int>>& M)
{
int r=M.size();
if(r==) return ;
int c=M[].size();
vector<vector<vector<int> > > dp(r,vector<vector<int>> (c,vector<int>(,) ) );
int res=;
for(int i=;i<r;i++)
for(int j=;j<c;j++)
{
if(M[i][j])
{
dp[i][j][]=j?dp[i][j-][]+:;
dp[i][j][]=i?dp[i-][j][]+:;
dp[i][j][]=i&&j?dp[i-][j-][]+:;
dp[i][j][]=i>&&j<c-?dp[i-][j+][]+:;
for(auto x:dp[i][j])
res=max(res,x);
}
}
return res;
}
};
LC 562. Longest Line of Consecutive One in Matrix的更多相关文章
- 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 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 ...
- LC 516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LC 3. Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LC 967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- LC 687. Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- [LC] 659. Split Array into Consecutive Subsequences
Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or ...
- [LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...
随机推荐
- 第十章、logging模块
目录 第十章.logging模块 一.logging模块及日志框架 第十章.logging模块 一.logging模块及日志框架 导入方式 import logging 作用 写日志 模块功能 # V ...
- Delphi BuildCommDCBAndTimeouts函数
- 0、CentOS7系统root密码丢失找回方法
第一步 1.重新启动或开启CentOS7.6系统,在选择进入系统的Grub菜单界面如下图1-1,根据提示按"e"小写字母进入编辑界面,如下图1-2所示: 图1-1 CentOS7. ...
- python如何编译py文件生成pyc、pyo、pyd以及如何和C语言结合使用
python执行py文件的流程 当我们执行一个py文件的时候,直接python xx.py即可,那么这个流程是怎么样的呢.先说明一下,python执行代码实际上是先打开文件然后执行里面的代码,所以文件 ...
- linux基础—课堂随笔_03 SHELL脚本编程基础
shell脚本编程基础 条件选择:if语句 选择执行: 注意:if语句可嵌套 单分支 if(开头)判断条件:then条件为真的分支代码 fi(结尾) 双分支 if(开头)判断条件:then条件为真的分 ...
- 【PKUSC2018】最大前缀和
上午的国庆大阅兵有意思 Description https://loj.ac/problem/6433 Solution 看数据范围认解法 首先在每种情况出现概率相同的情况下, \(期望 \times ...
- commons-codec-1.9.jar 是做什么用的?
commons-codec用来处理常用的编码方法的工具类包,例如DES.SHA1.MD5.Base64,URL,Soundx等等. 示例: 不可逆算法 1.MD5 String str = " ...
- PHP下查询游戏《Minecraft》多人游戏 服务器的人数。
废话不多说,直接上图: 作为一个优雅的Minecraft服务器,肯定需要官网的嗯. 很多服务器的官网都有显示当前服务器在线人数,延迟,每一个子服在线人数,甚至出了个流量图. 我们不搞花里胡哨的查询人数 ...
- WIF配置说明
<configuration> <configSections> <!--添加 WIF 4.5 sections :如下两条--> <section name ...
- k8s知识1
kubernetes到底有多难?看下面的白话: service 网络通信原理service 由k8s外面的服务作为访问端 内部里面其实是pod————————————————————————————— ...