在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a matrix of integers 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. A 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: 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: Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]] Output: 2 Example 3: 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:
给你一个 R 行 C 列的整数矩阵 路径沿四个基本方向(上、下、左、右)展开,从一个已访问单元格移动到任一相邻的未访问单元格。 路径的得分是该路径上的 最小 值。例如,路径 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 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 }
|
请发表评论