矩阵中最长连续递增子序列
2017-04-10 22:24
405 查看
Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
Return
The longest increasing path is
Example 2:
Return
The longest increasing path is
int x_1[4] = { 1, -1, 0, 0 };
int y_1[4] = { 0, 0, -1, 1 };
int dfs(vector<vector<int>>& matrix,int rows,int cols,
int row, int col, vector<vector<int>>&dp)
{
if (dp[row][col])
return dp[row][col];
int maxDepth = 1;
for (int i = 0; i < 4; ++i)
{
int x = row + x_1[i];
int y = col + y_1[i];
if (x < 0 || y < 0 || x >= rows
|| y >= cols || matrix[x][y] <= matrix[row][col])
continue;
int len = dfs(matrix, rows, cols, x, y, dp) + 1;
if (maxDepth < len)
maxDepth = len;
}
dp[row][col] = maxDepth;
return maxDepth;
}
int longestIncreasingPath(vector<vector<int>>& matrix)
{
if (matrix.empty())
return 0;
int rows = matrix.size();
int cols = matrix[0].size();
vector<vector<int>> dp(rows, vector<int>(cols, 0));
int maxDepth = 1;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
int tmp = dfs(matrix,rows,cols, i, j, dp);
if (maxDepth < tmp)
maxDepth = tmp;
}
}
return maxDepth;
}
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
nums = [ [9,9,4], [6,6,8], [2,1,1] ]
Return
4
The longest increasing path is
[1, 2, 6, 9].
Example 2:
nums = [ [3,4,5], [3,2,6], [2,2,1] ]
Return
4
The longest increasing path is
[3, 4, 5, 6]. Moving diagonally is not allowed.
int x_1[4] = { 1, -1, 0, 0 };
int y_1[4] = { 0, 0, -1, 1 };
int dfs(vector<vector<int>>& matrix,int rows,int cols,
int row, int col, vector<vector<int>>&dp)
{
if (dp[row][col])
return dp[row][col];
int maxDepth = 1;
for (int i = 0; i < 4; ++i)
{
int x = row + x_1[i];
int y = col + y_1[i];
if (x < 0 || y < 0 || x >= rows
|| y >= cols || matrix[x][y] <= matrix[row][col])
continue;
int len = dfs(matrix, rows, cols, x, y, dp) + 1;
if (maxDepth < len)
maxDepth = len;
}
dp[row][col] = maxDepth;
return maxDepth;
}
int longestIncreasingPath(vector<vector<int>>& matrix)
{
if (matrix.empty())
return 0;
int rows = matrix.size();
int cols = matrix[0].size();
vector<vector<int>> dp(rows, vector<int>(cols, 0));
int maxDepth = 1;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
int tmp = dfs(matrix,rows,cols, i, j, dp);
if (maxDepth < tmp)
maxDepth = tmp;
}
}
return maxDepth;
}
相关文章推荐
- 线段树区间合并+最长连续递增子序列——HDU 3308
- 求数组中最长连续递增子序列
- 最长连续递增子序列
- 5-2 最长连续递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- 最长单调"连续"递增子序列
- 用二分法寻找最长连续单调递增子序列
- 【EPI-6.6】最长连续递增子序列-启发式搜索
- 湖北民族学院oj 1668 之 最长连续递增子序列
- 51Nod - 1241 特殊的排序(求最长连续递增子序列)
- 习题3.4 最长连续递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
- 5-1 最长连续递增子序列
- 天梯赛2 重现5-4 最长连续递增子序列
- 最长连续递增子序列
- HDU 3308——LCIS(线段树,区间合并,最长连续递增子序列)