leetcode-并查集
-
题目:130

并查集:
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
f = {}
def find(x):
f.setdefault(x,x)
if f[x]!=x:
f[x] = find(f[x])
return f[x]
def union(x,y):
f[find(y)] = find(x)
if not board or not board[0]:
return
row,col = len(board),len(board[0])
dummy = row*col
for i in range(row):
for j in range(col):
if board[i][j] == "O":
if i == 0 or i == row - 1 or j == 0 or j == col - 1:
union(i * col + j, dummy)
else:
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if board[i + x][j + y] == "O":
union(i * col + j, (i + x) * col + (j + y)) for i in range(row):
for j in range(col):
if find(dummy) == find(i * col + j):
board[i][j] = "O"
else:
board[i][j] = "X"
题200:

并查集
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
f = {}
def find(x):
f.setdefault(x,x)
if f[x]!=x:
f[x] = find(f[x])
return f[x]
def union(x,y):
f[find(y)] = find(x) if not grid:
return 0
row,col =len(grid),len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] == "":
for x, y in [[-1, 0], [0, -1]]:
tmp_i = i + x
tmp_j = j + y
if 0 <= tmp_i < row and 0 <= tmp_j < col and grid[tmp_i][tmp_j] == "":
union(tmp_i * col + tmp_j, i * col + j)
res = set()
for i in range(row):
for j in range(col):
if grid[i][j] == "":
res.add(find(col*i+j))
return len(res)
题目684:


方法:并查集 O(N) O(N)

from collections import defaultdict
class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
f = {}
def find(x):
f.setdefault(x,x)
if f[x] != x:
f[x] = find(f[x])
return f[x]
def union(x,y):
if find(x) != find(y):
f[find(y)] = find(x)
for x,y in edges:
if find(x) == find(y):
return [x,y]
else:
union(x,y)

另:

class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
p = {i: {i} for i in range(1, len(edges) + 1)} #并查集初始化
for x, y in edges:
if p[x] is not p[y]: #如果两个集合地址不一样
p[x] |= p[y] #合并集合
for z in p[y]:
p[z] = p[x] #修改元素集合标记的指针地址
else:
return [x, y]
题685


并查集;
class Solution:
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
def find(f,x):
f.setdefault(x,x)
if f[x] != x:
f[x] = find(f,f[x])
return f[x]
def cycle(graph):
f = {}
for x,y in graph:
if find(f,x) == find(f,y):
return True
else:
f[find(f,y)] = find(f,x)
indegree = {i:0 for i in range(1,len(edges)+1)}
tmp = 0
for i,j in edges:
indegree[j] += 1
if indegree[j] == 2:
tmp = j
break
if tmp == 0:
f = {}
for x,y in edges:
if find(f,x) == find(f,y):
return [x,y]
else:
f[find(f,y)] = find(f,x)
else:
for x,y in edges[::-1]:
if y == tmp:
if not cycle(edges[:edges.index([x,y])]+edges[edges.index([x,y])+1:]) :
return [x,y]
leetcode-并查集的更多相关文章
- [leetcode] 并查集(Ⅰ)
预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...
- [leetcode] 并查集(Ⅱ)
最长连续序列 题目[128]:链接. 解题思路 节点本身的值作为节点的标号,两节点相邻,即允许合并(x, y)的条件为x == y+1 . 因为数组中可能会出现值为 -1 的节点,因此不能把 root ...
- [leetcode] 并查集(Ⅲ)
婴儿名字 题目[Interview-1707]:典型并查集题目. 解题思路 首先对 names 这种傻 X 字符串结构进行预处理,转换为一个 map,key 是名字,val 是名字出现的次数. 然后是 ...
- Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)
Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- leetcode 76 dp& 强连通分量&并查集经典操作
800. Similar RGB Color class Solution { int getn(int k){ return (k+8)/17; } string strd(int k){ char ...
- Java实现 LeetCode 721 账户合并(并查集)
721. 账户合并 给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails ...
- C#LeetCode刷题-并查集
并查集篇 # 题名 刷题 通过率 难度 128 最长连续序列 39.3% 困难 130 被围绕的区域 30.5% 中等 200 岛屿的个数 38.4% 中等 547 朋友圈 45.1% ...
随机推荐
- 2018-8-10-win10-UWP-button
title author date CreateTime categories win10 UWP button lindexi 2018-08-10 19:16:53 +0800 2018-2-13 ...
- RK3288编译 Android 5.1 固件
1 准备工作 编译 Android 对机器的配置要求较高: 64 位 CPU 16GB 物理内存+交换内存 30GB 空闲的磁盘空间用于构建,源码树另外占用大约 25GB Ubuntu 14.04 操 ...
- javaSE Comparable接口中的compareTo()方法
我们都知道,要对自建对象按照一定规则进行排序的话,要求自建对象实现Comparable接口,并重写compareTo() 方法,但compareTo() 方法的释义却不是那么容易搞清楚,下面举例进行阐 ...
- Tips using Manjaro
Set swappiness value The default swappiness value is set 60 as you can check it via the following co ...
- Codeforces 1175E 倍增
题意:给你n个区间和m次询问,每次询问一个区间[l, r]至少需要几个区间覆盖? 思路:如果只有一个区间,并且区间是整个取值范围的话,这是一个经典的区间覆盖问题,我们都知道贪心可以解决.现在我们需要快 ...
- 视频专家之路【四】:ffmpeg简单实战之获取属性
本文是听了雷宵骅大神的课之后的总结,部分内容借用了其PPT的内容,如有侵权请告知删除. 雷宵骅大神的博客为:https://blog.csdn.net/leixiaohua1020 本节的目的正式开始 ...
- 关于_getattr_方法的一些理解
在学习rest framework的过程中,rest framework的request是经过重构的,但是如果调用重构对象request中的属性,如果属性不存在会调用原request对象中的属性,它使 ...
- rest framework 之前
在开始rest framework之前,我们先来了解一下什么是restful rest 是一种软件架构风格,Representational state Transfer 它从资源的角度去看整个网络, ...
- 【NOI2019模拟2019.6.29】字符串(SA|SAM+主席树)
Description: 1<=n<=5e4 题解: 考虑\(f\)这个东西应该是怎样算的? 不妨建出SA,然后按height从大到小启发式合并,显然只有相邻的才可能成为最优答案.这样的只 ...
- noip1998 提高组t3 挖地雷
题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...