在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a 2D An "axis-aligned plus sign of Examples of Axis-Aligned Plus Signs of Order k: Order 1: 000 010 000 Order 2: 00000 00100 01110 00100 00000 Order 3: 0000000 0001000 0001000 0111110 0001000 0001000 0000000 Example 1: Input: N = 5, mines = [[4, 2]] Output: 2 Explanation: 11111 11111 11111 11111 11011 In the above grid, the largest plus sign can only be order 2. One of them is marked in bold. Example 2: Input: N = 2, mines = [] Output: 1 Explanation: There is no plus sign of order 2, but there is of order 1. Example 3: Input: N = 1, mines = [[0, 0]] Output: 0 Explanation: There is no plus sign, so return 0. Note:
在一个大小在 (0, 0) 到 (N-1, N-1) 的2D网格 一个 k" 阶由 k 阶轴对称加号标志示例: 阶 1: 000 010 000 阶 2: 00000 00100 01110 00100 00000 阶 3: 0000000 0001000 0001000 0111110 0001000 0001000 0000000 示例 1: 输入: N = 5, mines = [[4, 2]] 输出: 2 解释: 11111 11111 11111 11111 11011 在上面的网格中,最大加号标志的阶只能是2。一个标志已在图中标出。 示例 2: 输入: N = 2, mines = [] 输出: 1 解释: 11 11 没有 2 阶加号标志,有 1 阶加号标志。 示例 3: 输入: N = 1, mines = [[0, 0]] 输出: 0 解释: 0 没有加号标志,返回 0 。 提示:
Runtime: 1368 ms
Memory Usage: 19.8 MB
1 class Solution { 2 func orderOfLargestPlusSign(_ N: Int, _ mines: [[Int]]) -> Int { 3 var res:Int = 0 4 var dp:[[Int]] = [[Int]](repeating:[Int](repeating:N,count:N),count:N) 5 for mine in mines 6 { 7 dp[mine[0]][mine[1]] = 0 8 } 9 for i in 0..<N 10 { 11 var l:Int = 0 12 var r:Int = 0 13 var u:Int = 0 14 var d:Int = 0 15 var j:Int = 0 16 var k:Int = N - 1 17 while(j < N) 18 { 19 l = dp[i][j] != 0 ? l + 1 : 0 20 dp[i][j] = min(dp[i][j],l) 21 u = dp[j][i] != 0 ? u + 1 : 0 22 dp[j][i] = min(dp[j][i],u) 23 r = dp[i][k] != 0 ? r + 1 : 0 24 dp[i][k] = min(dp[i][k],r) 25 d = dp[k][i] != 0 ? d + 1 : 0 26 dp[k][i] = min(dp[k][i],d) 27 28 j += 1 29 k -= 1 30 } 31 } 32 for k in 0..<(N * N) 33 { 34 res = max(res, dp[k / N][k % N]) 35 } 36 return res 37 } 38 }
|
请发表评论