[Swift]LeetCode329. 矩陣中的最長遞增路徑 | Longest Increasing Path in a Matrix

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bvfmnqrq-ka.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an integer matrix, find the length of the longest increasing path.git

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).github

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:ide

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

給定一個整數矩陣,找出最長遞增路徑的長度。spa

對於每一個單元格,你能夠往上,下,左,右四個方向移動。 你不能在對角線方向上移動或移動到邊界外(即不容許環繞)。code

示例 1:htm

輸入: nums = 
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
] 
輸出: 4 
解釋: 最長遞增路徑爲 。[1, 2, 6, 9]

示例 2:blog

輸入: 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 }

404msget

 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 }
相關文章
相關標籤/搜索