关闭

【Leetcode】Longest Increasing Path in a Matrix

2410人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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.

思路:

本题实质是用dsp搜索最长路径。不过没必要对图中每个节点都用dsp求一遍最长路径,参考拓扑排序只从入度为0的节点开始dsp,这样做oj判断超时,那就再优化一下,用空间换时间,在dsp过程中记忆每个节点作为起点的最长路径的长度。

具体步骤为

1.根据矩阵构建邻接表,并记录和更新节点的入度。构建邻接表时,要注意满足题意即:当某节点小于上下左右节点才有一条相应路径。

2.dsp搜索,搜索入口为入度为0的结点。

构建邻接表时间复杂度为O(n^2),空间复杂度为O(n^2)。

算法:

	HashMap<Integer, List<Integer>> maps = new HashMap<Integer, List<Integer>>();// 有向图的邻接表
	int lens[];// 记住每个节点作为起点的最大长度

	public int longestIncreasingPath(int[][] matrix) {
		if (matrix.length == 0)
			return 0;
		
		int rows = matrix.length, cols = matrix[0].length;
		int[] digree = new int[rows * cols];// 入度
		lens = new int[rows * cols];
		
		// 初始化邻接表
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				List<Integer> neibors = new LinkedList<Integer>();
				if (i - 1 >= 0 && matrix[i - 1][j] > matrix[i][j]) {// 上
					neibors.add((i - 1) * cols + j);
					digree[(i - 1) * cols + j]++;
				}
				if (i + 1 < rows && matrix[i + 1][j] > matrix[i][j]) {// 下
					neibors.add((i + 1) * cols + j);
					digree[(i + 1) * cols + j]++;
				}
				if (j - 1 >= 0 && matrix[i][j - 1] > matrix[i][j]) {// 左
					neibors.add(i * cols + (j - 1));
					digree[i * cols + (j - 1)]++;
				}
				if (j + 1 < cols && matrix[i][j + 1] > matrix[i][j]) {// 右
					neibors.add(i * cols + (j + 1));
					digree[i * cols + (j + 1)]++;
				}
				maps.put(i * cols + j, neibors);
			}

		}
		int max = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (digree[i * cols + j] == 0) {//入度为0的节点为搜索入口
					int len = dsp(i * cols + j, 0);
					max = Math.max(max, len);
				}
			}
		}
		return max;
	}

	public int dsp(int s, int depth) {
		if (lens[s] != 0)
			return lens[s];

		List<Integer> neibors = maps.get(s);
		int max = 0;
		for (int n : neibors) {
			lens[n] = dsp(n, depth++);
			max = Math.max(max, lens[n]);
		}
		return max + 1;
	}


2
0
查看评论

leetcode笔记:Longest Increasing Path in a Matrix

题目的大意是,给定一个整数矩阵,计算其要求元素排列是递增的,球最长递增路径的长度。从任意一个矩阵位置出发,可向上下左右四个方向移动。不可以沿着对角线移动,也不能离开边界。(环绕也是不允许的)。题目还给出了两个测试用例。
  • liyuefeilong
  • liyuefeilong
  • 2016-02-21 23:41
  • 1702

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...
  • beiyetengqing
  • beiyetengqing
  • 2016-01-31 00:26
  • 980

[LeetCode] 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 dow...
  • u011331383
  • u011331383
  • 2016-01-28 20:49
  • 2406

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 either move to four directions: left, right, up or...
  • zmq570235977
  • zmq570235977
  • 2016-06-05 13:38
  • 444

[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 either move to four directions: left, right, up or...
  • sbitswc
  • sbitswc
  • 2016-02-21 08:49
  • 1437

【LEETCODE】300-Longest Increasing Subsequence [Python]

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longes...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-01-07 20:36
  • 869

leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)

参考 Python 解法: 动态规划 —— 最长递增子序列(LIS) 原题位置:Longest Increasing Subsequence | LeetCode OJ题目的说明: 严格递增; 子序列不要求连续; 解法 1,O(n2)O(n^2)class Solution { public: ...
  • lanchunhui
  • lanchunhui
  • 2016-06-08 11:33
  • 785

LeetCode 300. Longest Increasing Subsequence 解题报告

300. Longest Increasing Subsequence My Submissions Question Total Accepted: 17302 Total Submissions: 51952 Difficulty:&...
  • bruce128
  • bruce128
  • 2016-02-16 19:35
  • 3358

【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 either move to four directions: left, right, up...
  • Artemis96
  • Artemis96
  • 2016-10-02 13:54
  • 124

Leetcode Longest Increasing Path in a Matrix

题意:在矩阵中找最大递增路径。 思路:DFS。向可能的四个方向搜索,将返回的最大值 + 1作为该结点的值。 class Solution { public: vector > m; int xx,yy; vector > mmatrix; int lo...
  • markpen
  • markpen
  • 2016-12-22 09:22
  • 101
    个人资料
    • 访问:567784次
    • 积分:8660
    • 等级:
    • 排名:第2679名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论