[Swift]LeetCode329. 矩阵中的最长递增路径 | 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:

Input: nums =
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].

Example 2:

Input: nums =
[
  [3,4,5],
  [3,2,6],
  [2,2,1]
]
Output: 4
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.


给定一个整数矩阵,找出最长递增路径的长度。

对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。

示例 1:

输入: nums =
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]

示例 2:

输入: nums =
[
  [3,4,5],
  [3,2,6],
  [2,2,1]
]
输出: 4
解释: 最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。

 284 ms
 1 class Solution {
 2    func longestIncreasingPath(_ matrix: [[Int]]) -> Int {
 3
 4         if matrix.isEmpty || matrix[0].isEmpty {
 5             return 0
 6         }
 7
 8         let m = matrix.count
 9         let n = matrix[0].count
10
11         var counts = Array(repeating: Array(repeating: 0, count: n), count: m)
12         var longest = 0
13
14         for i in 0..<m {
15             for j in 0..<n {
16                 let c = longestHelp(matrix, &counts, i, j)
17                 longest = max(longest, c)
18             }
19         }
20
21         return longest
22     }
23
24     func longestHelp(_ matrix: [[Int]], _ counts : inout [[Int]], _ x : Int , _ y : Int) ->Int  {
25         if counts[x][y] != 0 {
26             return counts[x][y]
27         }
28
29         var res = 1
30         if x > 0 && matrix[x-1][y] > matrix[x][y] {
31             res = max(res, longestHelp(matrix, &counts, x-1, y) + 1)
32         }
33         if x < matrix.count-1 && matrix[x+1][y] > matrix[x][y] {
34             res = max(res, longestHelp(matrix, &counts, x+1, y) + 1)
35         }
36
37         if y > 0 && matrix[x][y-1] > matrix[x][y] {
38             res = max(res, longestHelp(matrix, &counts, x, y-1) + 1)
39         }
40         if y < matrix[0].count-1 && matrix[x][y+1] > matrix[x][y] {
41             res = max(res, longestHelp(matrix, &counts, x, y+1) + 1)
42         }
43
44         counts[x][y] = res
45         return res
46     }
47 }


404ms

 1 class Solution {
 2     var dp: [[Int]] = []
 3     let dirs: [[Int]] = [[-1, 0], [0, -1], [1, 0], [0, 1]]
 4     func longestIncreasingPath(_ matrix: [[Int]]) -> Int {
 5         if matrix.count == 0 { return 0 }
 6         dp = [[Int]](repeating: [Int](repeating: -1, count: matrix[0].count), count: matrix.count)
 7         var res = 0
 8         for r in 0..<matrix.count {
 9             for c in 0..<matrix[0].count {
10                 res = max(res, dfs(matrix, r, c))
11             }
12         }
13
14         return res
15     }
16
17     private func dfs(_ matrix: [[Int]], _ r: Int, _ c: Int) -> Int {
18         if self.dp[r][c] != -1 {
19             return dp[r][c]
20         }
21
22         var res = 0
23         for dir in dirs {
24             let newr  = r+dir[0], newc =  c + dir[1]
25             if newr >= 0 && newc >= 0 &&
26                 newr < matrix.count && newc < matrix[0].count &&
27                 matrix[newr][newc] > matrix[r][c] {
28                     res = max(res, dfs(matrix, newr, newc))
29                 }
30
31         }
32         dp[r][c] = res+1
33         return res + 1
34     }
35 }

原文地址:https://www.cnblogs.com/strengthen/p/10260831.html

时间: 2024-07-31 12:52:42

[Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix的相关文章

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

LeetCode. 矩阵中的最长递增路径

题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1]] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9]. class Solution { public: int dx[5] = {-1, 0, 1, 0}; int dy[5] = {0, 1, 0, -1}; int longest

329. 矩阵中的最长递增路径

题目: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1]] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].示例 2: 输入: nums = [ [3,4,5], [3,2,6], [2,2,1]] 输出: 4 解释: 最长递增路径是 [3, 4, 5, 6].注意不允许在对角线方向上移动

【python-leetcode329-深度优先搜索】矩阵中的最长递增路径

给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1]] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].示例 2: 输入: nums = [ [3,4,5], [3,2,6], [2,2,1]] 输出: 4 解释: 最长递增路径是 [3, 4, 5, 6].注意不允许在对角线方向上移动. cl

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 down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).Exa

算法实践--最长递增子序列(Longest Increasing Subsquence)

什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5}, {1} 其中最长的递增子序列是{2, 4, 5} 问题:对于长度为N的矢量D,如何找到它的最长递增子序列 一个简单的算法 for (i=N; i>0; --i) {1. 找到所有长度为i的子序列; //复杂度为(N!)/(i!)(N-i)! O(exp(N)) 2. 判断是否其中有一个为递增子

最长递增子序列(Longest increasing subsequence)

问题定义: 给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续). 这道题共3种解法. 1. 动态规划 动态规划的核心是状态的定义和状态转移方程.定义lis(i),表示前i个数中以A[i]结尾的最长递增子序列的长度.可以得到以下的状态转移方程: d(i) = max(1, d(j) + 1), 其中j < i,且A[j] <= A[i] 程序实现: int longestIncreasingSubsequence(vector<int> nums) { if (nu

矩阵中的最长递增子序列

和上一道  最长上升子序列 思路一样,不过还是从最笨的方法开始吧,也算是记录一下思考过程. 最开始的想法是,对矩阵的每个点都来一次回溯,得到从这个点开始的最长上升子序列长度.回溯的思路就是对上下左右遍历,利用到递归,停止遍历的条件是四周的数都比它大.代码如下: class Solution1 { public: // 对矩阵的每个点直接利用回溯法 超时了 void FindMaxLength(vector< vector<int> >& matrix, vector<

51nod1274 最长递增路径

将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define clr(x,c) memset(x,c,sizeof(