`

Longest Increasing Path in a Matrix

阅读更多
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+memory的方法,就是在DFS的同时,记录当前元素所能构成的最大长度,如果下次再访问到这个点的时候直接返回这个点在memory中的值就可以了。时间复杂度为O(m*n),空间复杂度也是O(m*n)。代码如下:
public class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        int[][] memory = new int[matrix.length][matrix[0].length];
        int max = 1;
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[0].length; j++) {
                max = Math.max(max, getLength(i, j, Integer.MIN_VALUE, memory, matrix));
            }
        }
        return max;
    }
    
    public int getLength(int i, int j, int min, int[][] memory, int[][] matrix) {
        if(i < 0 || j < 0 || i == matrix.length || j == matrix[0].length || matrix[i][j] <= min)
            return 0;
        if(memory[i][j] != 0) 
            return memory[i][j];
        min = matrix[i][j];
        int a = getLength(i - 1, j, min, memory, matrix) + 1;
        int b = getLength(i + 1, j, min, memory, matrix) + 1;
        int c = getLength(i, j + 1, min, memory, matrix) + 1;
        int d = getLength(i, j - 1, min, memory, matrix) + 1;
        memory[i][j] = Math.max(a, Math.max(b, Math.max(c, d)));
        return memory[i][j]; 
    }
}
分享到:
评论

相关推荐

    java-leetcode题解之Longest Increasing Path in a Matrix.java

    java java_leetcode题解之Longest Increasing Path in a Matrix.java

    算法导论--Introduction.to.Algorithms

    The revised third edition notably adds a chapter on van Emde Boas trees, one of the most useful data structures, and on multithreaded algorithms, a topic of increasing importance." —Daniel Spielman,...

    The Algorithm Design Manual (2rd Edition)

    8.3 Longest Increasing Sequence 8.4 War Story: Evolution of the Lobster 8.5 The Partition Problem 8.6 Parsing Context-Free Grammars 8.7 Limitations of Dynamic Programming: TSP 8.8 War Story: ...

    LeetCode

    "Shortest Path in Binary Matrix"则需要BFS寻找矩阵中最短路径。 总结,通过LeetCode的实践,我们可以提升JavaScript编程技巧,熟练掌握各种数据结构和算法,这对于个人职业发展和团队协作有着重要的意义。不断...

    LeetCode最全代码

    The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...

    POJ解题报告(ACM,PKU)从容易题到技巧题

    4. **1953 - "Longest Increasing Subsequence"** 长度最长递增子序列问题是一道经典的动态规划题目。通过维护一个动态数组,记录每个元素能构成的最长递增子序列的长度,可以求解此问题。 5. **3427 - "K-th ...

    leetCode:Leetcode解决方案

    例如,Binary Tree Preorder Traversal(二叉树前序遍历)和Shortest Path in Binary Matrix(二进制矩阵中最短路径)等。 二、算法策略 1. 搜索:深度优先搜索(DFS)和广度优先搜索(BFS)是解决许多问题的有效...

    全面的算法代码库

    最长上升子序列(n·log(n)) Longest-Increasing-Subsequence(n·log(n)) 倍增法求最近公共祖先 Lowest-Common-Ancestor(Doubling) 朴素的矩阵乘法 Matrix-Multiplication(Naive) 归并排序 Merge-Sort 最小堆 ...

    Skiena-The_Algorithm_Design_Manual.pdf

    8.3 Longest Increasing Sequence . . . . . . . . . . . . . . . . . . . . . 289 8.4 War Story: Evolution of the Lobster . . . . . . . . . . . . . . . . 291 8.5 The Partition Problem . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics