-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLoud and Rich.cpp
42 lines (34 loc) · 969 Bytes
/
Loud and Rich.cpp
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
34
35
36
37
38
39
40
41
42
// Runtime: 309 ms (Top 15.91%) | Memory: 46.8 MB (Top 48.36%)
class Solution {
public:
int dfs(int node,vector<int> &answer,vector<int> adjList[],vector<int>& quiet)
{
if(answer[node]==-1)
{
answer[node] = node;
for(int child:adjList[node])
{
int cand = dfs(child,answer,adjList,quiet);
if(quiet[cand]<quiet[answer[node]])
answer[node] = cand;
}
}
return answer[node];
}
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<int> adjList[n];
for(auto x:richer)
{
int v = x[0];
int u = x[1];
adjList[u].push_back(v);
}
vector<int> answer(n,-1);
for(int node =0;node<n;node++)
{
dfs(node,answer,adjList,quiet);
}
return answer;
}
};