LeetCode 934. Shortest Bridge
原题链接在这里: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 1
s not connected to any other 1s.)
Now, we may change 0
s to 1
s so as to connect the two islands together to form 1 island.
Return the smallest number of 0
s 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 <= A.length = A[0].length <= 100
A[i][j] == 0
orA[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的更多相关文章
- [LeetCode] 934. Shortest Bridge 最短的桥梁
In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected grou ...
- LC 934. Shortest Bridge
In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected grou ...
- 【LeetCode】934. Shortest Bridge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...
- 【leetcode】934. Shortest Bridge
题目如下: In a given 2D binary array A, there are two islands. (An island is a 4-directionally connecte ...
- Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)
Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- [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 ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
- [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 ...
随机推荐
- Linux低延迟服务器系统调优
最近做了一些系统和网络调优相关的测试,达到了期望的效果,有些感悟.同时,我也发现知乎上对Linux服务器低延迟技术的讨论比较欠缺(满嘴高并发现象):或者对现今cpu + 网卡的低延迟潜力认识不足(动辄 ...
- Akka-CQRS(7)- CQRS Reader Actor 示范
我们在这篇通过一个具体CQRS-Reader-Actor的例子来示范akka-persistence的query端编程和应用.在前面的博客里我们设计了一个CQRS模式POS机程序的操作动作录入过程,并 ...
- Java学习:JDBC各类详解
JDBC各个类详解 代码实现: //1.导入驱动jar包 //2.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //3.获取数据库连对象 ...
- 常用Java API之Ramdom--用代码模拟猜数小游戏
常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...
- dotnet core 之 CORS使用示例
这里列举几个经过验证的可用的CORS使用示例, 方便在需要的时候可以直接使用 示例1 #region snippet2 public void ConfigureServices(IServiceCo ...
- 根据语义来选择:value-like传副本, pointer-like传引用
★ (一个成员)变量的 创建.初始化.赋值 “默认值”:内置类型的局部变量.内置类型的成员变量(未设置=initval;) 没有默认值! 若创建时不指定值,则不进行初始化.则其值未定义!!! “指定 ...
- 根父类:Object 类
一.Object类 Java中规定: 如果一个类没有显式声明它的父类(即没有写extends xx),那么默认这个类的父类就是java.lang.Object. 类 Object 是类层次结构的根类. ...
- JavaScript 事件(基础)
一.事件 事件:触发-响应机制. 二.事件三要素 1.事件源:触发事件的元素 2.事件名称:发送了什么方式的事件 3.事件处理程序:事件触发后要执行的代码(函数形式) 三.事件的使用方式 1.获取元素 ...
- openssl生成证书及签名
第一步,生成私钥 $ openssl genrsa -out privatekey.pem 2048 查看生成的私钥内容 $ file privatekey.pem privatekey.pem: P ...
- php与阿里云短信接口接入
使用阿里云短信API,需要在控制台获取以下必要参数,其中需要自己手机验证+官方审核多次,尤其审核需要保持耐心. 1. accessKeyId 相当于你的个人账户密钥: 2. accessKeySec ...