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.

&nbsp;
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]


&nbsp;
Constraints:


	n == quiet.length
	1 &lt;= n &lt;= 500
	0 &lt;= quiet[i] &lt; n
	All the values of quiet are unique.
	0 &lt;= richer.length &lt;= n * (n - 1) / 2
	0 &lt;= ai, bi &lt; n
	ai != bi
	All the pairs of richer are unique.
	The observations in richer are all logically consistent.


class Solution:
    def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
        length = len(quiet)
        arr = [i for i in range(length)]
        indegree = [0 for _ in range(length)]
        graph = collections.defaultdict(list)
        dq = collections.deque([])
        
        for a, b in richer:
            # Note that the graph is uni-directional
            graph[a].append(b)
            indegree[b] += 1

        for i in range(length):
            if not indegree[i]: 
                dq.append(i)
    
        while dq:
            node = dq.popleft()
            
            for vertex in graph[node]:
                indegree[vertex] -= 1
                if quiet[arr[node]] < quiet[arr[vertex]]:
                    arr[vertex] = arr[node]
                if not indegree[vertex]:
                    dq.append(vertex)
        return arr
class Solution {
        ArrayList<ArrayList<Integer>> adj =new ArrayList<>();
        int res[];
    public int[] loudAndRich(int[][] richer, int[] quiet) {
        int n=quiet.length;
        res=new int[n];
        Arrays.fill(res,-1);
         for(int i=0;i<n;i++)
            adj.add(new ArrayList<Integer>());
        for(int i=0;i<richer.length;i++)
        {
            if(adj.get(richer[i][1])==null)
                adj.add(new ArrayList<>());
            adj.get(richer[i][1]).add(richer[i][0]);
        }
        for(int i=0;i<n;i++)
            dfs(i,quiet);
        return res;
    }
    public int dfs(int node ,int[] quiet)
    {
        if(res[node]==-1)
        {
            res[node]=node;
            for(int v:adj.get(node)){
                int cand=dfs(v,quiet);
                if(quiet[cand]<quiet[res[node]])
                res[node]=cand;
            }
        }
        return res[node];
    }
}
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;
    }
};
var loudAndRich = function(richer, quiet) {
    const map = new Map();
    for (const [rich, poor] of richer) {
        map.set(poor, (map.get(poor) || new Set()).add(rich));        
    }
    
    const memo = new Map();
    const getQuietest = (person) => {
        if (memo.has(person)) return memo.get(person);
        const richerList = map.get(person);
        let min = quiet[person];
        let quietest = person;
        if (!richerList) {
            memo.set(person, quietest);
            return quietest;
        }
        for (const rich of richerList) {         
            if (quiet[getQuietest(rich)] < min) {
                min = quiet[getQuietest(rich)];
                quietest = getQuietest(rich);
            }            
        }
        memo.set(person, quietest);
        return quietest;
    }
    const answer = [];
    for (let i=0; i<quiet.length; i++) {
        answer.push(getQuietest(i));
    }
    return answer;
};
Loud and Rich