Loud and Rich LeetCode solution in C++

1
class Solution {
2
public:
3
int dfs(int node, vector<int> &answer, vector<int> adjList[], vector<int> &quiet) {
4
if (answer[node] == -1) {
5
answer[node] = node;
6
for (int child : adjList[node]) {
7
int cand = dfs(child, answer, adjList, quiet);
8
if (quiet[cand] < quiet[answer[node]]) answer[node] = cand;
9
}
10
}
11

12
return answer[node];
13
}
14
vector<int> loudAndRich(vector<vector<int>> &richer, vector<int> &quiet) {
15
int n = quiet.size();
16

17
vector<int> adjList[n];
18

19
for (auto x : richer) {
20
int v = x[0];
21
int u = x[1];
22
adjList[u].push_back(v);
23
}
24

25
vector<int> answer(n, -1);
26

27
for (int node = 0; node < n; node++) {
28
dfs(node, answer, adjList, quiet);
29
}
30

31
return answer;
32
}
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0