[Swift]LeetCode764. 最大加號標誌 | Largest Plus Sign

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

In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0. What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign. If there is none, return 0.git

An "axis-aligned plus sign of 1s of order k" has some center grid[x][y] = 1 along with 4 arms of length k-1 going up, down, left, and right, and made of 1s. This is demonstrated in the diagrams below. Note that there could be 0s or 1s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1s. github

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

Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:
There is no plus sign, so return 0. 

Note:code

  1. N will be an integer in the range [1, 500].
  2. mines will have length at most 5000.
  3. mines[i] will be length 2 and consist of integers in the range [0, N-1].
  4. (Additionally, programs submitted in C, C++, or C# will be judged with a slightly smaller time limit.)

在一個大小在 (0, 0) 到 (N-1, N-1) 的2D網格 grid 中,除了在 mines 中給出的單元爲 0,其餘每一個單元都是 1。網格中包含 1 的最大的軸對齊加號標誌是多少階?返回加號標誌的階數。若是未找到加號標誌,則返回 0。htm

一個 k" 階由 1 組成的「軸對稱」加號標誌具備中心網格  grid[x][y] = 1 ,以及4個從中心向上、向下、向左、向右延伸,長度爲 k-1,由 1 組成的臂。下面給出 k" 階「軸對稱」加號標誌的示例。注意,只有加號標誌的全部網格要求爲 1,別的網格可能爲 0 也可能爲 1。 blog

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 。 

提示:

  1. 整數N 的範圍: [1, 500].
  2. mines 的最大長度爲 5000.
  3. mines[i] 是長度爲2的由2個 [0, N-1] 中的數組成.
  4. (另外,使用 C, C++, 或者 C# 編程將以稍小的時間限制進行​​判斷.)

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