[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)

Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.

Return the smallest number of 0s that must be flipped.  (It is guaranteed that the answer is at least 1.)

Example 1:

Input: [[0,1],[1,0]]
Output: 1

Example 2:

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

Example 3:

Input: [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
Output: 1

Note:

  1. 1 <= A.length = A[0].length <= 100
  2. A[i][j] == 0 or A[i][j] == 1


在给定的二维二进制数组 A 中,存在两座岛。(岛是由四面相连的 1 形成的一个最大组。)

现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。

返回必须翻转的 0 的最小数目。(可以保证答案至少是 1。)

示例 1:

输入:[[0,1],[1,0]]
输出:1

示例 2:

输入:[[0,1,0],[0,0,0],[0,0,1]]
输出:2

示例 3:

输入:[[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
输出:1

提示:

  1. 1 <= A.length = A[0].length <= 100
  2. A[i][j] == 0 或 A[i][j] == 1


648ms

  1 class Solution {
  2     func shortestBridge(_ A: [[Int]]) -> Int {
  3         var n:Int = A.count, m = A[0].count
  4         var can:[[Bool]] = [[Bool]](repeating: [Bool](repeating: false, count: m), count: n)
  5         var dr:[Int] = [1, 0, -1, 0 ]
  6         var dc:[Int] = [0, 1, 0, -1 ]
  7         var d:[[Int]] = [[Int]](repeating: [Int](repeating: 99999999, count: m), count: n)
  8
  9         var gq:Queue<[Int]> = Queue<[Int]>()
 10         inner:
 11         for i in 0..<n
 12         {
 13             for j in 0..<m
 14             {
 15                 if A[i][j] == 1
 16                 {
 17                     var q:Queue<[Int]> = Queue<[Int]>()
 18                     q.enQueue([i,j])
 19                     can[i][j] = true
 20                     while(!q.isEmpty())
 21                     {
 22                         var cur:[Int] = q.deQueue()!
 23                         gq.enQueue(cur)
 24                         var r:Int = cur[0], c:Int = cur[1]
 25                         d[r][c] = 0
 26                         for k in 0..<4
 27                         {
 28                             var nr:Int = r + dr[k], nc:Int = c + dc[k]
 29                             if nr >= 0 && nr < n && nc >= 0 && nc < m && A[nr][nc] == 1 && !can[nr][nc]
 30                             {
 31                                 can[nr][nc] = true
 32                                  q.enQueue([nr, nc])
 33                             }
 34                         }
 35                     }
 36                     break inner
 37                 }
 38             }
 39         }
 40
 41         while(!gq.isEmpty())
 42         {
 43             var cur:[Int] = gq.deQueue()!
 44             var r:Int = cur[0], c:Int = cur[1]
 45             for k in 0..<4
 46             {
 47                 var nr:Int = r + dr[k], nc:Int = c + dc[k]
 48                 if nr >= 0 && nr < n && nc >= 0 && nc < m && d[nr][nc] > d[r][c] + 1
 49                 {
 50                     d[nr][nc] = d[r][c] + 1
 51                     gq.enQueue([nr, nc])
 52                 }
 53             }
 54         }
 55         var ret:Int = 9999999
 56         for i in 0..<n
 57         {
 58             for j in 0..<m
 59             {
 60                 if !can[i][j] && A[i][j] == 1
 61                 {
 62                     ret = min(ret, d[i][j])
 63                 }
 64             }
 65         }
 66         return ret-1
 67     }
 68 }
 69
 70 public struct Queue<T> {
 71
 72     // 泛型数组:用于存储数据元素
 73     fileprivate var queue: [T]
 74
 75     // 返回队列中元素的个数
 76     public var count: Int {
 77         return queue.count
 78     }
 79
 80     // 构造函数:创建一个空的队列
 81     public init() {
 82         queue = [T]()
 83     }
 84
 85     //通过既定数组构造队列
 86     init(_ arr:[T]){
 87         queue = arr
 88     }
 89
 90     // 如果定义了默认值,则可以在调用函数时省略该参数
 91     init(_ elements: T...) {
 92         queue = elements
 93     }
 94
 95     // 检查队列是否为空
 96     // - returns: 如果队列为空,则返回true,否则返回false
 97     public func isEmpty() -> Bool {
 98         return queue.isEmpty
 99     }
100
101     // 入队列操作:将元素添加到队列的末尾
102     public mutating func enQueue(_ element: T) {
103         queue.append(element)
104     }
105
106     // 出队列操作:删除并返回队列中的第一个元素
107     public mutating func deQueue() -> T? {
108         return queue.removeFirst()
109     }
110 }

原文地址:https://www.cnblogs.com/strengthen/p/9903939.html

时间: 2024-10-08 16:01:30

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge的相关文章

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence

You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the s

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

[Swift Weekly Contest 111]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have a string "abcdef" and dele