0851. 喧闹和富有【中等】
1. 📝 题目描述
有一组 n 个人作为实验对象,从 0 到 n - 1 编号,其中每个人都有不同数目的钱,以及不同程度的安静值(quietness)。为了方便起见,我们将编号为 x 的人简称为 "person x "。
给你一个数组 richer,其中 richer[i] = [ai, bi] 表示 person ai 比 person bi 更有钱。另给你一个整数数组 quiet,其中 quiet[i] 是 person i 的安静值。richer 中所给出的数据 逻辑自洽(也就是说,在 person x 比 person y 更有钱的同时,不会出现 person y 比 person x 更有钱的情况 )。
现在,返回一个整数数组 answer 作为答案,其中 answer[x] = y 的前提是,在所有拥有的钱肯定不少于 person x 的人中,person y 是最不安静的人(也就是安静值 quiet[y] 最小的人)。
示例 1:
txt
输入: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
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
示例 2:
txt
输入:richer = [], quiet = [0]
输出:[0]1
2
2
提示:
n == quiet.length1 <= n <= 5000 <= quiet[i] < nquiet的所有值 互不相同0 <= richer.length <= n * (n - 1) / 20 <= ai, bi < nai != biricher中的所有数对 互不相同- 对
richer的观察在逻辑上是一致的
2. 🎯 s.1 - DFS + 记忆化
c
int* quietArr;
int* result;
int dfs(int** graph, int* graphSize, int u) {
if (result[u] != -1) return result[u];
result[u] = u;
for (int i = 0; i < graphSize[u]; i++) {
int cand = dfs(graph, graphSize, graph[u][i]);
if (quietArr[cand] < quietArr[result[u]]) result[u] = cand;
}
return result[u];
}
int* loudAndRich(int** richer, int richerSize, int* richerColSize, int* quiet, int quietSize, int* returnSize) {
int n = quietSize;
int** graph = (int**)malloc(sizeof(int*) * n);
int* graphSize = (int*)calloc(n, sizeof(int));
int* graphCap = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) { graph[i] = (int*)malloc(sizeof(int) * 4); graphCap[i] = 4; }
for (int i = 0; i < richerSize; i++) {
int a = richer[i][0], b = richer[i][1];
if (graphSize[b] == graphCap[b]) { graphCap[b] *= 2; graph[b] = realloc(graph[b], sizeof(int) * graphCap[b]); }
graph[b][graphSize[b]++] = a;
}
result = (int*)malloc(sizeof(int) * n);
memset(result, -1, sizeof(int) * n);
quietArr = quiet;
for (int i = 0; i < n; i++) dfs(graph, graphSize, i);
*returnSize = n;
for (int i = 0; i < n; i++) free(graph[i]);
free(graph); free(graphSize); free(graphCap);
return result;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
js
/**
* @param {number[][]} richer
* @param {number[]} quiet
* @return {number[]}
*/
var loudAndRich = function (richer, quiet) {
const n = quiet.length
const graph = Array.from({ length: n }, () => [])
for (const [a, b] of richer) graph[b].push(a)
const res = new Array(n).fill(-1)
const dfs = (u) => {
if (res[u] !== -1) return res[u]
res[u] = u
for (const v of graph[u]) {
const cand = dfs(v)
if (quiet[cand] < quiet[res[u]]) res[u] = cand
}
return res[u]
}
for (let i = 0; i < n; i++) dfs(i)
return res
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
py
class Solution:
def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
n = len(quiet)
graph = [[] for _ in range(n)]
for a, b in richer:
graph[b].append(a)
res = [-1] * n
def dfs(u: int) -> int:
if res[u] != -1: return res[u]
res[u] = u
for v in graph[u]:
cand = dfs(v)
if quiet[cand] < quiet[res[u]]:
res[u] = cand
return res[u]
for i in range(n): dfs(i)
return res1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 时间复杂度:
,其中 V 是人数,E 是关系数 - 空间复杂度:
算法思路:
- 建图:每个人指向比自己富有的人
- DFS 记忆化:对每个人找其及所有更富的人中最安静的