leetcode - 934. Shortest Bridge

Description

You are given an n x n binary matrix grid where 1 represents land and 0 represents water.

An island is a 4-directionally connected group of 1’s not connected to any other 1’s. There are exactly two islands in grid.

You may change 0’s to 1’s to connect the two islands to form one island.

Return the smallest number of 0’s you must flip to connect the two islands.

Example 1:

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

Example 2:

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

Example 3:

Input: grid = [[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

Constraints:

n == grid.length == grid[i].length
2 <= n <= 100
grid[i][j] is either 0 or 1.
There are exactly two islands in grid.

Solution

Use DFS to find one island, then use BFS to expand the island, until it meets another island.

Time complexity: o ( m ∗ n ) o(m*n) o(mn)
Space complexity: o ( m ∗ n ) o(m*n) o(mn)

Code

class Solution:def shortestBridge(self, grid: List[List[int]]) -> int:def dfs(x: int, y: int, island: list) -> None:stack = [(x, y)]while stack:x, y = stack.pop()if (x, y) in island:continueisland.append((x, y))for dz in (-1, 1):if 0 <= x + dz < m and grid[x + dz][y] == 1:stack.append((x + dz, y))if 0 <= y + dz < n and grid[x][y + dz] == 1:stack.append((x, y + dz))# get the 1s of an island# [(x1, y1), (x2, y2), ...]island = []m, n = len(grid), len(grid[0])for i in range(m):for j in range(n):if grid[i][j] == 1:dfs(i, j, island)breakif island:break# BFSqueue = collections.deque([(x, y, 0) for x, y in island])visited = set()while queue:x, y, step = queue.popleft()if (x, y) in visited:continuevisited.add((x, y))if grid[x][y] == 0:grid[x][y] = 1island.append((x, y))elif (x, y) not in island:return stepfor dz in (-1, 1):if 0 <= x + dz < m and (x + dz, y) not in island:next_step = step + (1 if grid[x + dz][y] == 0 else 0)queue.append((x + dz, y, next_step))if 0 <= y + dz < n and (x, y + dz) not in island:next_step = step + (1 if grid[x][y + dz] == 0 else 0)queue.append((x, y + dz, next_step))

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/623062.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【记录】重装系统后的软件安装

考完研重装了系统&#xff0c;安装软件乱七八糟&#xff0c;用到什么装什么。在这里记录一套标准操作&#xff0c;备用。一个个装还是很麻烦&#xff0c;我为什么不直接写个脚本直接下载安装包呢&#xff1f;奥&#xff0c;原来是我太菜了还不会写脚本啊&#xff01;先记着吧&a…

业务题day02

2-1 说一下生成课表的业务流程 当点击马上学习或者报名的时候&#xff0c;先去数据库查询课程是否存在或者是否在有效期内&#xff0c;如果判断通过&#xff0c;就成功。 接下来就要保存对应的课表&#xff0c;在上述操作中涉及两个微服务&#xff0c;下单过程trade微服务调用…

Zookeeper使用详解

介绍 ZooKeeper是一个分布式的&#xff0c;开放源码的分布式应用程序协调服务&#xff0c;是Google的Chubby一个开源的实现&#xff0c;是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件&#xff0c;提供的功能包括&#xff1a;配置维护、域名服务、分布…

C语言之冒泡排序(数组)

//数组--冒泡排序 //1.经典错误版 #include<stdio.h> void bubble_sort(int arr[]) { //确定冒泡函数的趟数(10个元素&#xff0c;9趟冒泡排序) int i 0; int sz sizeof(arr) / sizeof(arr[0]);//10 算出了1 这里错了 传参错了 for (i 0; i < sz…

1127: 矩阵乘积

题目描述 计算两个矩阵A和B的乘积。 输入 第一行三个正整数m、p和n&#xff0c;0<m,n,p<10&#xff0c;表示矩阵A是m行p列&#xff0c;矩阵B是p行n列&#xff1b; 接下来的m行是矩阵A的内容&#xff0c;每行p个整数&#xff0c;用空格隔开&#xff1b; 最后的p行是矩…

【零基础入门TypeScript】字符串

目录 句法 字符串方法 String 对象允许您处理一系列字符。它使用许多辅助方法包装字符串原始数据类型。 句法 var var_name new String(string); 下面给出了 String 对象中可用方法的列表及其描述&#xff1a; 编号属性及描述1.构造函数 返回对创建该对象的 String 函数的…

MFC多线程编程示例1

新建一个对话框工程; 添加2个编辑框,2个按钮; 对话框头文件添加, public:CWinThread *m_pthread1;CWinThread *m_pthread2;static UINT hellothread(LPVOID lparam);static UINT testthread(LPVOID lparam);CCriticalSection g_criticalsection;BOOL flag;int i1, i2; …

BC28 大小写转换

描述 实现字母的大小写转换。多组输入输出。 输入描述&#xff1a; 多组输入&#xff0c;每一行输入大写字母。 输出描述&#xff1a; 针对每组输入输出对应的小写字母。 示例1 输入&#xff1a; A B复制输出&#xff1a; a b 复制 备注&#xff1a; 多组输入过程中…

第九部分 使用函数 (四)

目录 一、foreach 函数 二、if 函数 三、call 函数 一、foreach 函数 foreach 函数和别的函数非常的不一样。因为这个函数是用来做循环用的&#xff0c;Makefile 中的 foreach 函数几乎是仿照于 Unix 标准 Shell&#xff08;/bin/sh&#xff09;中的 for 语句&#xff0c;或…

SpringFramework实战指南(一)

SpringFramework实战指南&#xff08;一&#xff09; 一、技术体系结构1.1 总体技术体系1.2 框架概念和理解 一、技术体系结构 1.1 总体技术体系 单一架构 一个项目&#xff0c;一个工程&#xff0c;导出为一个war包&#xff0c;在一个Tomcat上运行。也叫all in one。 单一架…

AUTOSAR从入门到精通-Autosar 中断机制(七)

目录 原理 任务与中断 任务 栈管理 激活任务 中断 copy数据及中断处理

springboot(ssm摄影分享网站系统 摄影论坛系统Java系统

springboot(ssm摄影分享网站系统 摄影论坛系统Java系统 开发语言&#xff1a;Java 框架&#xff1a;ssm/springboot vue JDK版本&#xff1a;JDK1.8&#xff08;或11&#xff09; 服务器&#xff1a;tomcat 数据库&#xff1a;mysql 5.7&#xff08;或8.0&#xff09; 数…

Kubernetes (十二) 存储——Volumes配置管理

一. 卷的概念 官方地址&#xff1a;卷 | Kuberneteshttps://v1-24.docs.kubernetes.io/zh-cn/docs/concepts/storage/volumes/ 二. 卷的类型及使用 …

前端性能优化之数据存取,存储以及缓存技术

无论是哪种计算机语言&#xff0c;说到底它们都是对数据的存取与处理。若能在处理数据前&#xff0c;更快地读取数据&#xff0c;那么必然会对程序执行性能产生积极的作用。 一般而言&#xff0c;js的数据存取有4种方式。 直接字面量:字面量不存储在特定位置也不需要索引&…

spring基于XML方式的组件管理

基本介绍 依赖注入是一种处理对象间依赖关系的技术。在Spring中&#xff0c;依赖注入有构造方法注入和设值注入两种方式。 设值注入是将依赖作为成员变量&#xff0c;通过主调类的setter方法注入依赖。构造方法注入则是在Bean的构造方法中注入依赖。 本次我们将通过具体例子来…

django分库分表的优化

Django分表方案 方案一&#xff1a;轮询方式分表 当系统数据越来越多的时候&#xff0c;查询变得缓慢&#xff0c;即使加了索引&#xff0c;由于表数据的增加&#xff0c;索引的维护也会成为数据库性能的限制问题&#xff0c;所以此时可以通过分表&#xff0c;将数据通过某种…

Java 虚拟机

运行时数据区域 方法区&#xff1a;方法区是线程共享的&#xff0c;用于存储已被虚拟机加载的类信息&#xff0c;常量&#xff0c;静态变量&#xff0c;即时编译器编译后的代码等数据。 虚拟机栈&#xff1a;虚拟机栈是线程私有的&#xff0c;其生命周期与线程相同即每个线…

CSC8021_computer network_The Transport Layer

Role of the transport layer • The transport layer is responsible for providing a reliable end-to-end connection between two application processes in a network • Abstracting away the physical subnet • Does not involve intermediate nodes • Takes a netwo…

UML-通信图和交互概览图(通信图和顺序图的区别与联系)

UML-通信图和交互概览图&#xff08;通信图和顺序图的区别与联系&#xff09; 一、通信图简介1.消息2.链接 二、通信图和[顺序图](https://blog.csdn.net/weixin_65032328/article/details/135587782)的联系与区别三、交互概览图四、顺序图转化为通信图练习 一、通信图简介 通…

2.2 物理层

2.2 物理层 2.2.1 物理层的基本概念 1、物理层主要解决在各种传输媒体上传输比特0和1的问题&#xff0c;进而给数据链路层提供透明传输比特流的服务 2、由于传输媒体的种类太多&#xff08;例如同轴电缆、光纤、无线电波等&#xff09;&#xff0c;物理连接方式也有很多例如…