当前位置: 首页 > news >正文

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.lryc.cn/news/280700.html

相关文章:

  • k8s的存储卷、数据卷
  • 流星全自动网页生成系统重构版源码
  • vscode打开c_cpp_properties.json文件的一种方式
  • 发起人自选-钉钉审批
  • 电脑DIY-显卡
  • vue3+vite+ts+pinia新建项目(略详细版)
  • 深入理解 Flink(五)Flink Standalone 集群启动源码剖析
  • SpringCloud Aliba-Nacos-从入门到学废【2】
  • web前端算法简介之字典与哈希表
  • 【uview2.0】Keyboard 键盘 与 CodeInput 验证码输入 结合使用 uview
  • 解决chromebook kabylake安装linux没有声音问题
  • Spring Boot - Application Events 的发布顺序_ApplicationContextInitializedEvent
  • 由jar包冲突导致的logback日志不输出
  • app开发——安卓native开发思路记录
  • 黑马程序员JavaWeb开发|案例:tlias智能学习辅助系统(1)准备工作、部门管理
  • C# .NET SQL sugar中 IsAny进行根据条件判断数据是否存在 IsAny的使用
  • 《Git学习笔记:Git入门 常用命令》
  • 小程序跳转安卓会跳转两次 iOS不会的解决方案
  • vue3+ts 中实现压缩图片、blob 转 base64
  • (框架设计-基础库建设) boost 库
  • 将ResultSet转实体类
  • Web后端开发
  • CAN201 计网概念收集
  • 【占用网络】FlashOcc:快速、易部署的占用预测模型
  • 239.【2023年华为OD机试真题(C卷)】求幸存者之和(模拟跳数-JavaPythonC++JS实现)
  • Pytorch中的标准维度顺序
  • Nginx的安装配置和使用
  • P1643 完美数 题解
  • docker一键安装
  • 模板管理支持批量操作,DataEase开源数据可视化分析平台v2.2.0发布