2Brute2Best
🔥0

Loud and Rich

Medium·LeetCode #851 Pro OnlySlight Variation
LeetCode
01

Problem Statement

There are n people labelled from 0 to n − 1. Each person has a different amount of money and a different quietness: quiet[i] is the quietness of person i, and no two people share a value. A lower quiet value means a quieter person.

richer[i] = [a, b] says that person a has more money than person b. The facts are consistent, so nobody is richer than themselves through a chain.

For each person x, find the quietest person among everyone we can prove has at least as much money as x. A person counts if a chain of richer pairs links them to x. Person x counts too. Return the answer for every person.

Input

richer, a list of pairs [a, b] (a is richer than b), and quiet, the quietness of each person.

Output

answer, one entry per person: the quietest person who is at least as rich as that person.

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]

Person 5 is richer than 3, who is richer than 1, who is richer than 0. Person 5 is the quietest of everyone who is at least as rich as person 0, so answer[0] = 5. For person 7, the people proven to be at least as rich are 3, 4, 5, 6 and 7 themselves. Person 7 is the quietest of them, so answer[7] = 7.

Example 2
Input: richer = [], quiet = [0]
Output: [0]

There is only one person, so that person is the answer.

Constraints
  • • 1 ≤ n ≤ 500
  • • quiet.length = n
  • • 0 ≤ quiet[i] < n
  • • All the values of quiet are unique.
  • • 0 ≤ richer.length ≤ n · (n − 1) / 2
  • • 0 ≤ aᵢ, bᵢ < n
  • • aᵢ ≠ bᵢ
  • • All the pairs of richer are unique.
  • • The observations in richer are all logically consistent.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“richer [a, b]”
→ Each pair is an arrow from the richer person to the poorer one. The people form a directed acyclic graph.
“equal to or more money than x”
→ Everyone who can reach x by walking up the arrows. That is a whole set, not only direct neighbours.
“quietest person”
→ A min-value question on each set. Combining sets is easy when the answers of the neighbours are already known.
03

Approach Complexity Ladder

Brute Force — walk up from each personO(N · (N + E))O(N + E)

For each x, search all the richer people and keep the quietest one

Optimal — memoised DFS: reuse the answers of richer peopleO(N + E)O(N + E)

Each person's answer is the quietest of themselves and their richer neighbours' answers; compute each one once

Brute Force Intuition

For each person x, collect everyone who is richer than x, directly or through a chain. Start a BFS at x, follow the richer pairs upward, and keep the person with the smallest quiet value that you reach.

Recognition rule for this sub-pattern:

Post-order DFS gives reverse topological order; memoised DFS computes values along a DAG.
Remember this · Topological Sort
Trigger
Prerequisites, dependencies, build order, "can all courses be finished?", ordering letters from sorted words.
Think
Is this a directed graph where some things must happen before others?
Key question
Which nodes currently have in-degree 0?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.