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 ...
随机推荐
- Effective.Java第67-77条(异常相关)
67. 明智审慎地进行优化 有三条优化的格言是每个人都应该知道的: (1)比起其他任何单一的原因(包括盲目的愚钝),很多计算上的过失都被归咎于效率(不一定能实现) (2)不要去计算效率上的一些小小的 ...
- c#创建windows服务(代码方式安装、启动、停止、卸载服务)
转载于:https://www.cnblogs.com/mq0036/p/7875864.html 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NE ...
- Reactive MySQL Client
Reactive MySQL Client是MySQL的客户端,具有直观的API,侧重于可伸缩性和低开销. 特征 事件驱动 轻量级 内置连接池 准备好的查询缓存 游标支持 行流 RxJava 1和Rx ...
- Java自学-接口与继承 UML图
UML 图 步骤 1 : UML 图 -- 类之间的关系 UML-Unified Module Language 统一建模语言,可以很方便的用于描述类的属性,方法,以及类和类之间的关系 步骤 2 : ...
- python基础知识(一)
Python基础知识 计算基础知识 1.cpu 人类的大脑 运算和处理问题 2.内存 临时存储数据 断电就消失了 3.硬盘 永久存储数据 4.操作系统 调度硬件设备之间数据交互 python的应用和历 ...
- Xcode11 Developer Tool中没了Application Loader
升级Xcode11之后不少人发现在Open Developer Tool中没了Application Loader. 那么如果我们还想用该怎么办呢? 先这样 找个老版的Xcode–>Conten ...
- 安全SECUERITY单词SECUERITY证券
中文名:证券业 外文名:secuerity 含义:指从事证券发行和交易服务 性质:证券市场的基本组成要素 组成:证券交易所.证券公司 目录 1 证券评级 2 证券定义 ? 涵义 ? 内容 ? 分类 ? ...
- uni-app结合PHP实现单用户登陆
单用户登陆,即在一个应用中,同一个用户只能在线登陆一个,一个用户登陆,在其他设备上会被即时挤下线,确认后清空登陆该设备上的登陆装填并退回到登陆界面. uni-app是目前能通过使用vue.js框架只需 ...
- 后缀表达式 Java实现
基本原理: 从左到右扫描字符串:1.是操作数:压栈. 2.是操作符:出栈两个操作数,将运算结果压栈. 扫描字符串通过java.util.Scanner类实现,其next方法可以读取以空格(默认)或指定 ...
- django应用之corsheaders[跨域设置]
安装 pip install django-cors-headers 注册应用 INSTALLED_APPS = ( ... 'corsheaders', ... ) 中间层设置 MIDDLEWARE ...