- 浏览: 185013 次
- 性别:
- 来自: 济南
文章分类
最新评论
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)。代码如下:
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]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 504Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 671Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 591Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 607Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 693Write a program to find the nth ... -
Coin Change
2016-02-29 04:39 790You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 724For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 447There are n bulbs that are init ...
相关推荐
java java_leetcode题解之Longest Increasing Path in a Matrix.java
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,...
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: ...
"Shortest Path in Binary Matrix"则需要BFS寻找矩阵中最短路径。 总结,通过LeetCode的实践,我们可以提升JavaScript编程技巧,熟练掌握各种数据结构和算法,这对于个人职业发展和团队协作有着重要的意义。不断...
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) ...
4. **1953 - "Longest Increasing Subsequence"** 长度最长递增子序列问题是一道经典的动态规划题目。通过维护一个动态数组,记录每个元素能构成的最长递增子序列的长度,可以求解此问题。 5. **3427 - "K-th ...
例如,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 最小堆 ...
8.3 Longest Increasing Sequence . . . . . . . . . . . . . . . . . . . . . 289 8.4 War Story: Evolution of the Lobster . . . . . . . . . . . . . . . . 291 8.5 The Partition Problem . . . . . . . . . . ...