英文原文
There is a group of n
people labeled from 0
to n - 1
where each person has a different amount of money and a different level of quietness.
You are given an array richer
where richer[i] = [ai, bi]
indicates that ai
has more money than bi
and an integer array quiet
where quiet[i]
is the quietness of the ith
person. All the given data in richer are logically correct (i.e., the data will not lead you to a situation where x
is richer than y
and y
is richer than x
at the same time).
Return an integer array answer
where answer[x] = y
if y
is the least quiet person (that is, the person y
with the smallest value of quiet[y]
) among all people who definitely have equal to or more money than the person x
.
Example 1:
Input: richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0] Output: [5,5,2,5,4,5,6,7] Explanation: answer[0] = 5. Person 5 has more money than 3, which has more money than 1, which has more money than 0. The only person who is quieter (has lower quiet[x]) is person 7, but it is not clear if they have more money than person 0. answer[7] = 7. Among all people that definitely have equal to or more money than person 7 (which could be persons 3, 4, 5, 6, or 7), the person who is the quietest (has lower quiet[x]) is person 7. The other answers can be filled out with similar reasoning.
Example 2:
Input: richer = [], quiet = [0] Output: [0]
Constraints:
n == quiet.length
1 <= n <= 500
0 <= quiet[i] < n
- All the values of
quiet
are unique. 0 <= richer.length <= n * (n - 1) / 2
0 <= ai, bi < n
ai != bi
- All the pairs of
richer
are unique. - The observations in
richer
are all logically consistent.
中文题目
在一组 N 个人(编号为 0, 1, 2, ..., N-1
)中,每个人都有不同数目的钱,以及不同程度的安静(quietness)。
为了方便起见,我们将编号为 x
的人简称为 "person x
"。
如果能够肯定 person x
比 person y
更有钱的话,我们会说 richer[i] = [x, y]
。注意 richer
可能只是有效观察的一个子集。
另外,如果 person x
的安静程度为 q
,我们会说 quiet[x] = q
。
现在,返回答案 answer
,其中 answer[x] = y
的前提是,在所有拥有的钱不少于 person x
的人中,person y
是最安静的人(也就是安静值 quiet[y]
最小的人)。
示例:
输入:richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0] 输出:[5,5,2,5,4,5,6,7] 解释: answer[0] = 5, person 5 比 person 3 有更多的钱,person 3 比 person 1 有更多的钱,person 1 比 person 0 有更多的钱。 唯一较为安静(有较低的安静值 quiet[x])的人是 person 7, 但是目前还不清楚他是否比 person 0 更有钱。 answer[7] = 7, 在所有拥有的钱肯定不少于 person 7 的人中(这可能包括 person 3,4,5,6 以及 7), 最安静(有较低安静值 quiet[x])的人是 person 7。 其他的答案也可以用类似的推理来解释。
提示:
1 <= quiet.length = N <= 500
0 <= quiet[i] < N
,所有quiet[i]
都不相同。0 <= richer.length <= N * (N-1) / 2
0 <= richer[i][j] < N
richer[i][0] != richer[i][1]
richer[i]
都是不同的。- 对
richer
的观察在逻辑上是一致的。
通过代码
官方题解
方法:缓存深度优先搜索法
思路
如果 y
比 x
富有,就认为在有向图中存在边 x -> y
。
对每个 x
(也就是每个人),我们都希望最安静的人就在 x
的子树中。
算法
构建上面所描述的图,并且 dfs(person)
是 person
的子树上最安静的人。注意,因为语句在逻辑上是一致的,所以图必须是有向无环图(即,DAG)—— 任意一条边都有方向,且不存在环路的图。
现在 dfs(person)
既可以是 person
本身,也可以是 min(dfs(child))
。也就是说,子树中最安静的人可以是 person
本身,或者是 person
的子结点的某个子树中最安静的人。
当执行图的 后序遍历 时,我们可以将 dfs(person)
的值缓存为 answer[person]
。这样,我们就不会重复工作。该技巧有助于将算法的时间复杂度从平方阶降低到线性阶。
class Solution {
ArrayList<Integer>[] graph;
int[] answer;
int[] quiet;
public int[] loudAndRich(int[][] richer, int[] quiet) {
int N = quiet.length;
graph = new ArrayList[N];
answer = new int[N];
this.quiet = quiet;
for (int node = 0; node < N; ++node)
graph[node] = new ArrayList<Integer>();
for (int[] edge: richer)
graph[edge[1]].add(edge[0]);
Arrays.fill(answer, -1);
for (int node = 0; node < N; ++node)
dfs(node);
return answer;
}
public int dfs(int node) {
if (answer[node] == -1) {
answer[node] = node;
for (int child: graph[node]) {
int cand = dfs(child);
if (quiet[cand] < quiet[answer[node]])
answer[node] = cand;
}
}
return answer[node];
}
}
class Solution(object):
def loudAndRich(self, richer, quiet):
N = len(quiet)
graph = [[] for _ in xrange(N)]
for u, v in richer:
graph[v].append(u)
answer = [None] * N
def dfs(node):
#Want least quiet person in this subtree
if answer[node] is None:
answer[node] = node
for child in graph[node]:
cand = dfs(child)
if quiet[cand] < quiet[answer[node]]:
answer[node] = cand
return answer[node]
return map(dfs, range(N))
复杂度分析
时间复杂度:${O}(N^2)$ ,其中 $N$ 为总人数。遍历
richer
数组,在每个新遍历到的人都比前一个更富有的情况下,该数组至多可以包含 $1 + … + N - 1 = N(N - 1) / 2$ 个元素。空间复杂度:${O}(N^2)$,用于维护一个有 $N^2$ 条边的图。
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
4761 | 9669 | 49.2% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|