题目
有 n 座城市,编号从 1 到 n 。编号为 x 和 y 的两座城市直接连通的前提是: x 和 y 的公因数中,至少有一个 严格大于 某个阈值 threshold 。更正式地说,如果存在整数 z ,且满足以下所有条件,则编号 x 和 y 的城市之间有一条道路:
x % z == 0
y % z == 0
z > threshold
给你两个整数 n 和 threshold ,以及一个待查询数组,请你判断每个查询 queries[i] = [ai, bi] 指向的城市 ai 和 bi 是否连通(即,它们之间是否存在一条路径)。
返回数组 answer ,其中answer.length == queries.length 。如果第 i 个查询中指向的城市 ai 和 bi 连通,则 answer[i] 为 true ;如果不连通,则 answer[i] 为 false 。
示例 1:
输入:n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]
输出:[false,false,true]
解释:每个数的因数如下:
1: 1
2: 1, 2
3: 1, 3
4: 1, 2, 4
5: 1, 5
6: 1, 2, 3, 6
所有大于阈值的的因数已经加粗标识,只有城市 3 和 6 共享公约数 3 ,因此结果是:
[1,4] 1 与 4 不连通
[2,5] 2 与 5 不连通
[3,6] 3 与 6 连通,存在路径 3--6
示例 2:
输入:n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]
输出:[true,true,true,true,true]
解释:每个数的因数与上一个例子相同。但是,由于阈值为 0 ,所有的因数都大于阈值。因为所有的数字共享公因数 1 ,所以所有的城市都互相连通。
示例 3:
输入:n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]
输出:[false,false,false,false,false]
解释:只有城市 2 和 4 共享的公约数 2 严格大于阈值 1 ,所以只有这两座城市是连通的。
注意,同一对节点 [x, y] 可以有多个查询,并且查询 [x,y] 等同于查询 [y,x] 。
提示:
2 <= n <= 10^4
0 <= threshold <= n
1 <= queries.length <= 10^5
queries[i].length == 2
1 <= ai, bi <= cities
ai != bi
解题思路
一开始的时候以为是求queries
中各个数的最大公因数,并且最大公因数> threshold
就返回True
,反之返回False
,后来发现不是,因为16
和9
可以连接,是通过16 - 12 - 18 - 9
连接的,所以这个思路不对。
根据示例1的提示,先求出来每个数字的因数,然后做一个因数-数字的倒排索引,然后把>threshold
的因数对应的所有数,并起来,最后query
其实就是看2个数是否在一个集合里。
看了其他人的思路发现,其实倒排索引这里可以省略掉,直接枚举threshold + 1
到n
的所有数字,将该数字的倍数并起来即可。
代码
倒排索引枚举因子版:
class UnionFindSet:
def __init__(self, n: int) -> None:
self.parent = list(range(n + 1))
self.height = [1] * (n + 1)
def find(self, x: int) -> int:
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x: int, y: int) -> None:
x, y = self.find(x), self.find(y)
if x == y:
return
if self.height[x] < self.height[y]:
self.parent[x] = y
else:
self.parent[y] = x
if self.height[x] == self.height[y]:
self.height[x] += 1
class Solution:
def areConnected(self, n: int, threshold: int, queries: List[List[int]]) -> List[bool]:
def get_factor(n: int) -> list:
ans = []
for i in range(1, int(sqrt(n)) + 1):
if n % i == 0:
ans += [i, n // i]
return ans
factor_dict = {}
for i in range(1, n + 1):
factors = get_factor(i)
for each_factor in factors:
if each_factor not in factor_dict:
factor_dict[each_factor] = []
factor_dict[each_factor].append(i)
union_find_set = UnionFindSet(n)
for factor in factor_dict.keys():
if factor <= threshold:
continue
nums = factor_dict[factor]
for sub_num in nums[1:]:
union_find_set.union(nums[0], sub_num)
return [union_find_set.find(x) == union_find_set.find(y) for x, y in queries]
枚举因子版:
class UnionFindSet:
def __init__(self, n: int) -> None:
self.parent = [i for i in range(n + 1)]
self.height = [1] * (n + 1)
def find(self, x: int) -> int:
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x: int, y: int) -> None:
x, y = self.find(x), self.find(y)
if x == y:
return
if self.height[x] < self.height[y]:
self.parent[x] = y
else:
self.parent[y] = x
if self.height[x] == self.height[y]:
self.height[x] += 1
class Solution:
def areConnected(self, n: int, threshold: int, queries: List[List[int]]) -> List[bool]:
union_find_set = UnionFindSet(n)
for factor in range(threshold + 1, n + 1):
for mul in range(factor * 2, n + 1, factor):
union_find_set.union(factor, mul)
return [union_find_set.find(x) == union_find_set.find(y) for x, y in queries]