Skip to content

Latest commit

 

History

History
121 lines (48 loc) · 1.57 KB

File metadata and controls

121 lines (48 loc) · 1.57 KB

中文文档

Description

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:

    <li><code>1 &lt;= A.length =&nbsp;A[0].length &lt;= 100</code></li>
    
    <li><code>A[i][j] == 0</code> or <code>A[i][j] == 1</code></li>
    
 

Solutions

Python3

Java

...