码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Increasing Path in a Matrix

时间:2016-10-29 07:51:49      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:find   决定   public   example   没有   als   时间   超时   就会   

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 public class Solution {
 2     public int longestIncreasingPath(int[][] matrix) {
 3         int[] max = new int[1];
 4         for (int i = 0; i < matrix.length; i++) {
 5             for (int j = 0; j < matrix[0].length; j++) {
 6                 boolean[][] visited = new boolean[matrix.length][matrix[0].length];
 7                 longestIncreasingPathHelper(matrix, visited, i, j, matrix[i][j] - 1, 0, max);
 8             }
 9         }
10         return max[0];
11     }
12 
13     public void longestIncreasingPathHelper(int[][] matrix, boolean[][] visited, int i, int j, int prev, int size,
14             int[] max) {
15         if (i < 0 || i >= matrix.length || j < 0 || j >= matrix[0].length)    return;
16 
17         if (visited[i][j] == true || matrix[i][j] <= prev) return;
18 
19         visited[i][j] = true;
20         size++;
21         max[0] = Math.max(max[0], size);
22         
23         longestIncreasingPathHelper(matrix, visited, i + 1, j, matrix[i][j], size, max);
24         longestIncreasingPathHelper(matrix, visited, i - 1, j, matrix[i][j], size, max);
25         longestIncreasingPathHelper(matrix, visited, i, j + 1, matrix[i][j], size, max);
26         longestIncreasingPathHelper(matrix, visited, i, j - 1, matrix[i][j], size, max);
27         visited[i][j] = false;
28     }
29 }

第二种方法类似第一种方法,但是我们不会每次都对同一个位置重复计算。对于一个点来讲,它的最长路径是由它周围的点决定的,你可能会认为,它周围的点也是由当前点决定的,这样就会陷入一个死循环的怪圈。其实并没有,因为我们这里有一个条件是路径上的值是递增的,所以我们一定能够找到一个点,它不比周围的值大,这样的话,整个问题就可以解决了。

 1 public class Solution {
 2     public int longestIncreasingPath(int[][] A) {
 3         int res = 0;
 4         if (A == null || A.length == 0 || A[0].length == 0) {
 5             return res;
 6         }
 7         int[][] store = new int[A.length][A[0].length];
 8         for (int i = 0; i < A.length; i++) {
 9             for (int j = 0; j < A[0].length; j++) {
10                 if (store[i][j] == 0) {
11                     res = Math.max(res, dfs(A, store, i, j));
12                 }
13             }
14         }
15         return res;
16     }
17 
18     private int dfs(int[][] a, int[][] store, int i, int j) {
19         if (store[i][j] != 0) {
20             return store[i][j];
21         }
22         int left = 0, right = 0, up = 0, down = 0;
23         if (j + 1 < a[0].length && a[i][j + 1] > a[i][j]) {
24             right = dfs(a, store, i, j + 1);
25         }
26         if (j > 0 && a[i][j - 1] > a[i][j]) {
27             left = dfs(a, store, i, j - 1);
28         }
29         if (i + 1 < a.length && a[i + 1][j] > a[i][j]) {
30             down = dfs(a, store, i + 1, j);
31         }
32         if (i > 0 && a[i - 1][j] > a[i][j]) {
33             up = dfs(a, store, i - 1, j);
34         }
35         store[i][j] = Math.max(Math.max(up, down), Math.max(left, right)) + 1;
36         return store[i][j];
37     }
38 }

 

Longest Increasing Path in a Matrix

标签:find   决定   public   example   没有   als   时间   超时   就会   

原文地址:http://www.cnblogs.com/beiyeqingteng/p/6009922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!