原题链接在这里:https://leetcode.com/problems/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

题解:

Find an index pointing to 1. Starting from it, iterating its neighbors and put all the islands into first set.

Perform BFS for each index in the current set, search surroundings, it is never visited before, and it is 1, then it is a cell in the other island, return level.

Otherwise, it is water, add it to nextSet.

Time Complexity: O(m*n). m = A.length. n = A[0].length.

Space: O(m*n).

AC Java:

 class Solution {
int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int shortestBridge(int[][] A) {
if(A == null || A.length == 0 || A[0].length == 0){
return -1;
} int m = A.length;
int n = A[0].length;
int r = 0;
int c = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(A[i][j] == 0){
continue;
} r = i;
c = j;
}
} boolean [][] visited = new boolean[m][n];
LinkedList<int []> que = new LinkedList<>();
HashSet<int []> beginSet = new HashSet<>();
visited[r][c] = true;
que.add(new int[]{r,c});
while(!que.isEmpty()){
int [] cur = que.poll();
beginSet.add(cur);
for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || A[x][y]!= 1 || visited[x][y]){
continue;
} visited[x][y] = true;
que.add(new int[]{x, y});
}
} int level = 0; while(!beginSet.isEmpty()){
HashSet<int []> nextSet = new HashSet<>();
for(int [] node : beginSet){
for(int [] dir : dirs){
int x = node[0] + dir[0];
int y = node[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || visited[x][y]){
continue;
} visited[x][y] = true;
if(A[x][y] == 1){
return level;
} nextSet.add(new int[]{x, y});
}
} level++;
beginSet = nextSet;
} return -1;
}
}

LeetCode 934. Shortest Bridge的更多相关文章

  1. [LeetCode] 934. Shortest Bridge 最短的桥梁

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  2. LC 934. Shortest Bridge

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  3. 【LeetCode】934. Shortest Bridge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...

  4. 【leetcode】934. Shortest Bridge

    题目如下: In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connecte ...

  5. Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)

    Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  6. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  7. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  9. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

随机推荐

  1. DELL OptiPlex 7050M黑苹果纪录

    准备工作: 主机:OptiPlex 7050 Micro Desktop Computer 镜像:黑果小兵 macOS Catalina 10.15.1 安装过程: 大体的安装过程,就Dell品牌而言 ...

  2. MySQL卸载与重装

    [卸载] 不推荐使用控制面板-->卸载程序,来卸载,容易出现报错. 使用电脑管家卸载很轻松. [删除注册列表] HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\S ...

  3. 修改host文件加速访问github

    修改本地电脑系统 hosts 文件C:\Windows\System32\drivers\etc,直接在最后加入以下代码 192.30.253.112 github.com 192.30.253.11 ...

  4. .net Dapper 实践系列(5) ---事务编辑(Layui+Ajax+Dapper+MySQL)

    目录 写在前面 实践步骤 写在前面 上一小节,我们总结了根据Id查询多表数据,最后返回Json对象给前台的例子.接下来,在这一小节我们要实现多表编辑的操作. 实践步骤 因为上一小节以及创建了Edit视 ...

  5. Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法

    Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法 一丶ORM常用字段 ...

  6. dubbo循序渐进 - 什么是RPC

    RPC的核心并不在于使用什么协议.RPC的目的是让你在本地调用远程的方法,而对你来说这个调用是透明的,你并不知道这个调用的方法是部署哪里.通过RPC能解耦服务,这才是使用RPC的真正目的.RPC的原理 ...

  7. Spring MVC Web.xml配置

    Web.xml spring&spring mvc 在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个 ...

  8. HTML 注释 和 实体字符

    一.注释 在HTML中还有一种特殊的标签——注释标签.如果需要在HTML文档中添加一些便于阅读和理解但又不需要显示在页面中的注释文字,就需要使用注释标签. 注释内容不会显示在浏览器窗口中,但是作为HT ...

  9. kvm虚拟化之virt-install

    1. 常用参数 -n --name= 客户端虚拟机名称 -r --ram= 客户端虚拟机分配的内存 -u --uuid= 客户端UUID 默认不写时,系统会自动生成 --vcpus= 客户端的vcpu ...

  10. Excel 逐条导入Mysql(数据更新)

    其实,我的业务流程是, 先读取excel/csv -> pandas 数据清洗 -> 导入Mysql, 一般是做一个表append 或者是 if exist -> replace的操 ...