[Leetcode] 329. Longest Increasing Path in a Matrix 解题报告

来源:互联网 发布:英语步步高下载软件 编辑:程序博客网 时间:2024/06/16 06:57

题目

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.

思路

1、BFS:由于越小的数字其递减链越长,所以我们先对数据元素进行排序,然后依次从大的数据元素开始处理:对于每个元素,一旦发现周围有比它小的元素,并且其目前的路径长度小于当前元素的最长路径(已经是最终值了,将来不会再更新,请思考为什么?)加1,则更新。最终返回所有路径中的最大值即可。

2、DFS:DFS的好处是不需要对数据元素进行有序处理,但是朴素的DFS也存在重复计算的问题:例如对于图中所给出的测试用例,在其最长路径9,6,2,1上,我们计算9的过程中就已经把6,2和1的路径都计算过了,但是在碰到6的时候还需要再重复计算一次。一个有效的解决方法就是采用DFS+记忆。一旦计算出来了某个数据元素的最长路径,则将其保存下来,这样在后面的计算中,如果发现该位置上的元素已经被计算出来了,则直接利用所保存的结果。DFS的代码写出来比BFS要简洁一些。

代码

1、BFS:

class Solution {public:    int longestIncreasingPath(vector<vector<int>>& matrix) {        if(matrix.size() == 0 || matrix[0].size() == 0) {            return 0;        }        int row_num = matrix.size(), col_num = matrix[0].size();        multimap<int, pair<int, int>> value_maps;        for(int i = 0; i < row_num; ++i) {            for(int j = 0; j < col_num; ++j) {                value_maps.insert(make_pair(matrix[i][j], make_pair(i, j)));            }        }        vector<vector<int>> paths(row_num, vector<int>(col_num, 1));        int max_path = 1;        for(auto it = value_maps.rbegin(); it != value_maps.rend(); ++it) {            int value = it->first;            int row = it->second.first, col = it->second.second;            if(row > 0 && matrix[row-1][col] < value) {                paths[row-1][col] = max(paths[row-1][col], paths[row][col] + 1);                max_path = max(max_path, paths[row-1][col]);            }            if(row < row_num - 1 && matrix[row+1][col] < value) {                paths[row+1][col] = max(paths[row+1][col], paths[row][col] + 1);                max_path = max(max_path, paths[row+1][col]);            }            if(col > 0 && matrix[row][col-1] < value) {                paths[row][col-1] = max(paths[row][col-1], paths[row][col] + 1);                max_path = max(max_path, paths[row][col-1]);            }            if(col < col_num - 1 && matrix[row][col+1] < value) {                paths[row][col+1] = max(paths[row][col+1], paths[row][col] + 1);                max_path = max(max_path, paths[row][col+1]);            }        }        return max_path;    }};
2、DFS:
class Solution {public:    int longestIncreasingPath(vector<vector<int>>& matrix) {                if(matrix.size() == 0 || matrix[0].size() == 0) {            return 0;        }        int row = matrix.size(), col = matrix[0].size();        int ret = 0;        for(int i = 0; i < row; ++i){            for(int j = 0; j < col; ++j) {                ret = max(ret, dfs(matrix, i, j, LONG_MIN));            }        }        return ret;    }private:    int dfs(vector<vector<int>>& matrix, int x, int y, long val) {        int row = matrix.size(), col = matrix[0].size();        if(x < 0 || x >= row || y < 0 || y >= col || matrix[x][y] <= val) {     // out of boundary or no-larger values            return 0;        }        if(hash.count(x * col + y)) {                                           // has already been calculated            return hash[x * col + y];        }        int len1 = dfs(matrix, x + 1, y, matrix[x][y]);        int len2 = dfs(matrix, x - 1, y, matrix[x][y]);        int len3 = dfs(matrix, x, y + 1, matrix[x][y]);        int len4 = dfs(matrix, x, y - 1, matrix[x][y]);        int max_len = max(max(len1, len2), max(len3, len4)) + 1;        hash[x * col + y] = max_len;        return max_len;    }    unordered_map<int, int> hash;};

原创粉丝点击