[Swift]LeetCode1102. 得分最高的路徑 | Path With Maximum Minimum Value

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

Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1].git

The score of a path is the minimum value in that path.  For example, the value of the path 8 →  4 →  5 →  9 is 4.github

path moves some number of times from one visited cell to any neighbouring unvisited cell in one of the 4 cardinal directions (north, east, west, south). 微信

Example 1:app

Input: [[5,4,5],[1,2,6],[7,4,6]]
Output: 4 Explanation: The path with the maximum score is highlighted in yellow. 

Example 2:spa

Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]]
Output: 2

Example 3:3d

Input: [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]
Output: 3 

Note:code

  1. 1 <= R, C <= 100
  2. 0 <= A[i][j] <= 10^9

給你一個 R 行 C 列的整數矩陣 A。矩陣上的路徑從 [0,0] 開始,在 [R-1,C-1] 結束。htm

路徑沿四個基本方向(上、下、左、右)展開,從一個已訪問單元格移動到任一相鄰的未訪問單元格。blog

路徑的得分是該路徑上的 最小 值。例如,路徑 8 →  4 →  5 →  9 的值爲 4 。

找出全部路徑中得分 最高 的那條路徑,返回其 得分。 

示例 1:

輸入:[[5,4,5],[1,2,6],[7,4,6]]
輸出:4
解釋: 
得分最高的路徑用黃色突出顯示。 

示例 2:

輸入:[[2,2,1,2,2,2],[1,2,2,2,1,2]]
輸出:2

示例 3:

輸入:[[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]
輸出:3 

提示:

  1. 1 <= R, C <= 100
  2. 0 <= A[i][j] <= 10^9

  

 1 class Solution {
 2     let dir:[[Int]] = [[0,1],[0,-1],[1,0],[-1,0]]
 3     func maximumMinimumPath(_ A: [[Int]]) -> Int {
 4         var A = A
 5         let n:Int = A.count
 6         let m:Int = A[0].count
 7         var l:Int = 0
 8         var r:Int = min(A[0][0], A[n-1][m-1])
 9         while (l < r)
10         {
11             let mid:Int = l + ((r-l)>>1) + 1
12             if check(mid, n, m, &A)
13             {
14                 l = mid
15             }
16             else
17             {
18                 r = mid - 1
19             }
20         }
21         return l
22     }
23     
24     func check(_ lim:Int,_ n:Int,_ m:Int,_ a:inout [[Int]]) ->Bool
25     {
26         var vis:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:m),count:n)
27         vis[0][0] = true
28         var qu:[[Int]] = [[Int]]()
29         qu.append([0,0])
30         while(!qu.isEmpty)
31         {
32             let arr:[Int] = qu.removeFirst()
33             let x:Int = arr[0]
34             let y:Int = arr[1]
35             
36             
37             for i in 0..<4
38             {
39                 let tx:Int = x + dir[i][0]
40                 let ty:Int = y + dir[i][1]
41                 if tx < 0 || tx >= n || ty < 0 || ty >= m || vis[tx][ty] || a[tx][ty] < lim
42                 {
43                     continue
44                 }
45                 if tx == n-1 && ty == m-1
46                 {
47                     return true
48                 }
49                 qu.append([tx, ty])
50                 vis[tx][ty] = true
51             }
52         }
53         return false
54     }
55 }
相關文章
相關標籤/搜索