您的位置:首页 > 其它

329. Longest Increasing Path in a Matrix

2017-09-25 08:30 288 查看

问题描述:

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:

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.

解题思路:

题目中要求求出在一个给定的矩阵中按照上下左右四个方向找出单调递增的最长的序列,显然,这样的问题可以抽象为寻找路径的问题。从某一个点出发,找出从该点出发可以得到的最长的递增序列,遍历矩阵中的所有的点,找到最长的路径。因此,也就是一个简单的DFS的问题。

而本题中,还可以做少许的优化。因为,从某一点可以获得的最长递增序列的长度是一定的。也即,可以通过一定的公式来减少重复的遍历:

有某点X,X的四个方向上的下一个点分别为X_left, X_right, X_up, X_down。
有LongestIncreasingLengthOf(X) =
max{LongestIncreasingLengthOf(X_left), LongestIncreasingLengthOf(X_right), LongestIncreasingLengthOf(X_up), LongestIncreasingLengthOf(X_down)} + 1


如此,只需要加上一个存储关于点的可以获取的最长递增序列的矩阵,以及一个标记该点是否已经获取过最长递增序列的矩阵,就可以减少重复的运算。思路与斐波那契数列存储前一次运算的值的想法相似。

代码:

#include <iostream>
#include <vector>

using namespace std;

int height, width;
int** LengthRecord;
bool** mark; //represent have found the longestIncreasingPath for this point

bool validator(int row, int col) {
if (row >= 0 && row < height && col >= 0 && col < width)
return true;
return false;
}

int findLongestIncreasingPath(vector<vector<int> >& matrix, int row, int col) {
int length = 0;
if (!mark[row][col]) {
int count = 0;
if (validator(row - 1, col) && matrix[row - 1][col] > matrix[row][col]) {
length = findLongestIncreasingPath(matrix, row - 1, col) + 1;
LengthRecord[row][col] = length > LengthRecord[row][col]? length : LengthRecord[row][col];
count++;
}
if (validator(row + 1, col) && matrix[row + 1][col] > matrix[row][col]) {
length = findLongestIncreasingPath(matrix, row + 1, col) + 1;
LengthRecord[row][col] = length > LengthRecord[row][col]? length : LengthRecord[row][col];
count++;
}
if (validator(row, col - 1) && matrix[row][col - 1] > matrix[row][col]) {
length = findLongestIncreasingPath(matrix, row, col - 1) + 1;
LengthRecord[row][col] = length > LengthRecord[row][col]? length : LengthRecord[row][col];
count++;
}
if (validator(row, col + 1) && matrix[row][col + 1] > matrix[row][col]) {
length = findLongestIncreasingPath(matrix, row, col + 1) + 1;
LengthRecord[row][col] = length > LengthRecord[row][col]? length : LengthRecord[row][col];
count++;
}
mark[row][col] = true;
if (count == 0) {
LengthRecord[row][col] = 1;
}
}
return LengthRecord[row][col];
}

int longestIncreasingPath(vector<vector<int> >& matrix) {
if (matrix.empty()) return 0;
int max_length = 0;
for (int i = 0 ; i < height; i++) {
for (int j = 0; j < width; j++) {
findLongestIncreasingPath(matrix, i, j);
max_length = max_length > LengthRecord[i][j]? max_length : LengthRecord[i][j];
}
}
return max_length;
}

int main() {
vector<vector<int> > matrix;
int temp;
cin >> height >> width;
LengthRecord = new int*[height];
mark = new bool*[height];
for (int i = 0; i < height; i++) {
LengthRecord[i] = new int[width];
mark[i] = new bool[width];
vector<int> temp_vector;
for (int j = 0; j < width; j++) {
mark[i][j] = false;
LengthRecord[i][j] = -1;
cin >> temp;
temp_vector.push_back(temp);
}
matrix.push_back(temp_vector);
}
cout << longestIncreasingPath(matrix) << endl;
}


Leetcode评测:



结束语:

题目本身不是很难,难的应该是把实际出现的问题转换为相应的数学模型,这就需要在平时的练习中多练习抽象解决问题的能力。转换为相应的模型并采取合适的方法后问题就可以迎刃而解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: