Skip to content

Commit e65052f

Browse files
author
robot
committed
feat: Maximize the Number of Equivalent Pairs After Swaps]
1 parent 7774bf1 commit e65052f

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
252252
- [Minimum Dropping Path Sum](./problems/Minimum-Dropping-Path-Sum.md)
253253
- [Longest-Matrix-Path-Length](./problems/Longest-Matrix-Path-Length.md)
254254
- [Every Sublist Min Sum](./problems/Every-Sublist-Min-Sum.md)
255+
- [Maximize the Number of Equivalent Pairs After Swaps](./problems/Maximize-the-Number-of-Equivalent-Pairs-After-Swaps.md)
255256

256257
- [0002. 两数相加](./problems/2.add-two-numbers.md)
257258
- [0003. 无重复字符的最长子串](./problems/3.longest-substring-without-repeating-characters.md)

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
- [Bus Fare](./problems/Bus-Fare.md) 👍
106106
- [Minimum Dropping Path Sum](./problems/Minimum-Dropping-Path-Sum.md)
107107
- [Every Sublist Min Sum](./problems/Every-Sublist-Min-Sum.md)
108+
- [Maximize the Number of Equivalent Pairs After Swaps](./problems/Maximize-the-Number-of-Equivalent-Pairs-After-Swaps.md)
108109
- [0002. 两数相加](./problems/2.add-two-numbers.md)
109110
- [0003. 无重复字符的最长子串](./problems/3.longest-substring-without-repeating-characters.md)
110111
- [0005. 最长回文子串](./problems/5.longest-palindromic-substring.md)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## 题目地址(690. Maximize the Number of Equivalent Pairs After Swaps)
2+
3+
https://binarysearch.com/problems/Maximize-the-Number-of-Equivalent-Pairs-After-Swaps
4+
5+
## 题目描述
6+
7+
```
8+
You are given a list of integers of the same length A and B. You are also given a two-dimensional list of integers C where each element is of the form [i, j] which means that you can swap A[i] and A[j] as many times as you want.
9+
10+
Return the maximum number of pairs where A[i] = B[i] after the swapping.
11+
12+
Constraints
13+
14+
n ≤ 100,000 where n is the length of A and B
15+
m ≤ 100,000 where m is the length of C
16+
Example 1
17+
Input
18+
A = [1, 2, 3, 4]
19+
B = [2, 1, 4, 3]
20+
C = [
21+
[0, 1],
22+
[2, 3]
23+
]
24+
Output
25+
4
26+
Explanation
27+
We can swap A[0] with A[1] then A[2] with A[3].
28+
```
29+
30+
## 前置知识
31+
32+
- 并查集
33+
- BFS
34+
- DFS
35+
36+
## 并查集
37+
38+
### 思路
39+
40+
这道题的核心在于如果 A 中的 [0,1] 可以互换,并且 [1,2] 可以互换,那么 [0,1,2] 可以任意互换。
41+
42+
也就是说互换**具有联通性**。这种联通性对做题有帮助么?有的!
43+
44+
根据 C 的互换关系,我们可以将 A 分为若干联通域。对于每一个联通域我们可以任意互换。因此我们可以枚举每一个联通域,对联通域中的每一个索引 i ,我们看一下 B 中是否有对应 B[j] == A[i] 其中 i 和 j 为同一个联通域的两个点。
45+
46+
具体算法:
47+
48+
- 首先根据 C 构建并查集。
49+
- 然后根据将每一个联通域存到一个字典 group 中,其中 group[i] = list,i 为联通域的元,list 为联通域的点集合列表。
50+
- 枚举每一个联通域,对联通域中的每一个索引 i ,我们看一下 B 中是否有对应 B[j] == A[i] 其中 i 和 j 为同一个联通域的两个点。累加答案即可
51+
52+
### 代码
53+
54+
代码支持:Python3
55+
56+
Python3 Code:
57+
58+
```py
59+
60+
class UF:
61+
def __init__(self, M):
62+
self.parent = {}
63+
self.cnt = 0
64+
# 初始化 parent,size 和 cnt
65+
for i in range(M):
66+
self.parent[i] = i
67+
self.cnt += 1
68+
69+
def find(self, x):
70+
if x != self.parent[x]:
71+
self.parent[x] = self.find(self.parent[x])
72+
return self.parent[x]
73+
return x
74+
def union(self, p, q):
75+
if self.connected(p, q): return
76+
leader_p = self.find(p)
77+
leader_q = self.find(q)
78+
self.parent[leader_p] = leader_q
79+
self.cnt -= 1
80+
def connected(self, p, q):
81+
return self.find(p) == self.find(q)
82+
83+
class Solution:
84+
def solve(self, A, B, C):
85+
n = len(A)
86+
uf = UF(n)
87+
for fr, to in C:
88+
print(fr, to)
89+
uf.union(fr, to)
90+
group = collections.defaultdict(list)
91+
92+
for i in uf.parent:
93+
group[uf.find(i)].append(i)
94+
ans = 0
95+
for i in group:
96+
indices = group[i]
97+
values = collections.Counter([A[i] for i in indices])
98+
for i in indices:
99+
if values[B[i]] > 0:
100+
values[B[i]] -= 1
101+
ans += 1
102+
return ans
103+
104+
```
105+
106+
**复杂度分析**
107+
108+
令 n 为数组 A 的长度,v 为图的点数,e 为图的边数。
109+
110+
- 时间复杂度:$O(n+v+e)$
111+
- 空间复杂度:$O(n)$
112+
113+
## 总结
114+
115+
我们也可以使用 BFS 或者 DFS 来生成 group,生成 group 后的逻辑大家都是一样的,这两种解法留给大家来实现吧。
116+
117+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
118+
119+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 46K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。

0 commit comments

Comments
 (0)