「这是我参与11月更文挑战的第28天,活动详情查看:2021最后一次更文挑战」
描述
We have n cities labeled from 1 to n. Two different cities with labels x and y are directly connected by a bidirectional road if and only if x and y share a common divisor strictly greater than some threshold. More formally, cities with labels x and y have a road between them if there exists an integer z such that all of the following are true:
- x % z == 0,
- y % z == 0, and
- z > threshold.
Given the two integers, n and threshold, and an array of queries, you must determine for each queries[i] = [ai, bi] if cities ai and bi are connected directly or indirectly. (i.e. there is some path between them).
Return an array answer, where answer.length == queries.length and answer[i] is true if for the ith query, there is a path between ai and bi, or answer[i] is false if there is no path.
Example 1:
1 | vbnet复制代码Input: n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]] |
Example 2:
1 | vbnet复制代码Input: n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]] |
Example 3:
1 | vbnet复制代码Input: n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]] |
Note:
- 2 <= n <= 10^4
- 0 <= threshold <= n
- 1 <= queries.length <= 10^5
- queries[i].length == 2
- 1 <= ai, bi <= cities
- ai != bi
解析
根据题意,给定 n 个城市,从 1 到 n 标记。 当且仅当 x 和 y 共享一个严格大于某个 threshold 的公约数时,标签为 x 和 y 的两个不同城市通过一条双向道路直接相连。 如果存在一个整数 z 使得以下所有条件都为真,则标签为 x 和 y 的城市之间有一条道路:
- x % z == 0
- y % z == 0
- z > threshold
给定两个整数 n 和 threshold ,以及一个数组 queries ,针对每个 queries[i] = [ai, bi] ,确认城市 ai 和 bi 是否相连接,不管直接还是间接。 返回一个数组 answer ,其中 answer.length == queries.length 并且 answer[i] 如果对于第 i 个查询,如果城市之间存在路径,则 answer[i] 为 true,如果没有路径,则 answer[i] 为 false。
本题其实考察的是快读选择两个城市进行合并,如果用暴力解法,先遍历 x 城市再遍历 y 城市,在判断是否相通,时间复杂度太高了。换一个思路就是遍历大于 threshold 的公约数 t ,然后在小于等于 n 范围内找 t 的倍数,只要是 t 的倍数的城市都可以合并到一块,给这些城市赋予最小的城市 id 作为他们的祖先。最后便利 queries 中的每一对城市,如果他们的祖先相同说明就是相通的,否则说明不相通。
解答
1 | python复制代码class Solution(object): |
运行结果
1 | erlang复制代码Runtime: 824 ms, faster than 100.00% of Python online submissions for Graph Connectivity With Threshold. |
原题链接:leetcode.com/problems/gr…
您的支持是我最大的动力
本文转载自: 掘金