> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Loud and Rich Python Solution with Tests

> Tested Python solution for LeetCode 851 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 851, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Depth-First Search](/catalog/topics/depth-first-search), [Graph Theory](/catalog/topics/graph-theory), [Topological Sort](/catalog/topics/topological-sort), Directed Acyclic Graph. [View on LeetCode](https://leetcode.com/problems/loud-and-rich/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 851   # by problem number
lcpy gen -s loud_and_rich   # by problem name
```

## Problem

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`.

### Examples

```
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.
```

```
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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/loud_and_rich/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/loud_and_rich/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + e)
    # Space: O(n + e)
    def loud_and_rich(self, richer: list[list[int]], quiet: list[int]) -> list[int]:
        n = len(quiet)
        graph: list[list[int]] = [[] for _ in range(n)]
        for a, b in richer:
            graph[b].append(a)

        answer: list[int] = [-1] * n

        def dfs(x: int) -> int:
            if answer[x] != -1:
                return answer[x]
            best = x
            for y in graph[x]:
                cand = dfs(y)
                if quiet[cand] < quiet[best]:
                    best = cand
            answer[x] = best
            return best

        return [dfs(i) for i in range(n)]
```

## Complexity

| Time | Space |
| - | - |
| O(n + e) | O(n + e) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.