[leetcode] 329. Longest Increasing Path in a Matrix

原创 2016年03月07日 23:29:11

题目:
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,此处无需记录哪些节点已经被访问过,因为我们要求的是递增的序列,比如a,b,c,d,当访问到e的时候不需要去考虑a,b,c,d是否已经访问过,因为e肯定比a,b,c,d都大,所以a,b,c,d在大小上不会符合要求,所以我们无需保存哪些节点已经访问过。使用动态规划保存已经求得的值。DP[x][y]代表的是从x,y开始最长的递增序列。如果这个x,y节点的DP值还没有算出来,那么我们只需要从当前节点往不同的四个方向分别遍历,采用回溯的DFS方法,如果这个节点已经算出了DP值,那么直接返回。所以当前节点的最长递增长度等于从四个方向中挑出一个最长的加上自身。
以上。
代码如下:

class Solution {
public:
    int longestIncreasingPath(vector<vector<int>>& matrix) {
        if(matrix.size() == 0)return 0;
        int max_value = 0;
        this->matrix = matrix;
        row = matrix.size();
        col = matrix[0].size();
        this->DP = vector<vector<int>>(row, vector<int>(col, 0));
        directions = {{0,1}, {0,-1}, {-1,0}, {1, 0}};
        for(int i = 0; i < row; i++)
            for(int j = 0; j < col; j++) {
                int res = backTracking(i, j);
                max_value = max(max_value, res);
            }
        return max_value;
    }

    int backTracking(int x, int y) {
        if(DP[x][y])return DP[x][y];
        int result = 0;
        for(auto& direction:directions) {
            int nextX = x + direction[0];
            int nextY = y + direction[1];
            if(nextX < 0 || nextX >= row || nextY < 0 || nextY >= col || matrix[x][y] >= matrix[nextX][nextY])continue;
            int res = backTracking(nextX, nextY);
            result = max(result, res);
        }
        DP[x][y] = ++result;
        return result;
    }
    private:
    vector<vector<int>> matrix;
    vector<vector<int>> DP;
    int row;
    int col;
    vector<vector<int>> directions;
};

相关文章推荐

Leetcode 329. Longest Increasing Path in a Matrix

-题目Equations are given in the format A / B = k, where A and B are variables represented as strings, ...

#leetcode#329. Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...

leetcode-329. Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith...
  • toyijiu
  • toyijiu
  • 2016年05月08日 22:53
  • 120

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

题目: Given an integer matrix, find the length of the longest increasing path. From each cell,...

LeetCode #329 - Longest Increasing Path in a Matrix - Hard

ProblemGiven an integer matrix, find the length of the longest increasing path.From each cell, you c...
  • Arcome
  • Arcome
  • 2016年12月21日 20:29
  • 150

【LEETCODE】329-Longest Increasing Path in a Matrix [Python]

Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...

leetcode解题之329. Longest Increasing Path in a Matrix Java版 (最长上升路径)

leetcode解题之329. Longest Increasing Path in a Matrix Java版 (最长上升路径)

Leetcode 329. Longest Increasing Path in a Matrix[hard]

题目: Given an integer matrix, find the length of the longest increasing path.From each cell, you can...

LeetCode 329: Longest Increasing Path in a Matrix

329. Longest Increasing Path in a MatrixDifficulty: Hard Given an integer matrix, find the length o...

leetcode 329. Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 329. Longest Increasing Path in a Matrix
举报原因:
原因补充:

(最多只允许输入30个字)