Imagine a social circle where wealth creates a hierarchy, but quietness is the most valued trait. You're given information about n people, each with different amounts of money and varying levels of quietness.

You have an array richer where richer[i] = [a, b] means person a has more money than person b. You also have an array quiet where quiet[i] represents how quiet person i is (lower values mean quieter).

Your task: For each person, find the quietest person among all people who have equal or more money than them. This could be themselves if they're the quietest in their wealth tier!

Example: If person 0 has $100, person 1 has $200, and person 2 has $150, and their quietness levels are [5, 2, 8], then for person 0, we need to find the quietest among {person 1, person 2} since both are richer. Person 1 with quietness 2 would be the answer.

Input & Output

example_1.py โ€” Basic Hierarchy
$ 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]
๐Ÿ’ก Note: For person 0: People 1,2,3,4,5,6 are richer. Among them, person 5 has the lowest quiet value (1). For person 1: People 2,3,4,5,6 are richer. Person 5 is the quietest. And so on...
example_2.py โ€” Simple Case
$ Input: richer = [[0,1],[1,2]], quiet = [5,3,1]
โ€บ Output: [0,1,2]
๐Ÿ’ก Note: Person 2 has no one richer, so answer is themselves. Person 1 has person 0 richer (quiet=5) vs themselves (quiet=3), so answer is themselves. Person 0 has no one richer, so answer is themselves.
example_3.py โ€” Single Person
$ Input: richer = [], quiet = [0]
โ€บ Output: [0]
๐Ÿ’ก Note: Only one person exists, and no one is richer than anyone else. So the answer for person 0 is themselves.

Visualization

Tap to expand
Corporate Hierarchy: Find the Most Humble SuperiorAlice$50kBob$80kCarol$120kDave$200kEve$150kHumility: 8Humility: 5Humility: 3Humility: 9Humility: 6Solution Process:โ€ข For Alice: DFS finds {Dave, Eve} โ†’ Dave is most humble (9)โ€ข For Bob: DFS finds {Dave, Eve} โ†’ Eve is most humble (6)โ€ข Memoization ensures each person is processed only once!
Understanding the Visualization
1
Build Hierarchy
Create a graph where arrows point from poorer to richer people
2
DFS Exploration
For each person, explore all paths to richer people using DFS
3
Find Quietest
Among all reachable richer people (including self), select the quietest
4
Memoization
Cache results to avoid recalculating for overlapping subproblems
Key Takeaway
๐ŸŽฏ Key Insight: By modeling wealth as a directed graph and using DFS with memoization, we can efficiently find the quietest person in each person's wealth hierarchy without redundant calculations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Each person is processed at most once due to memoization, and each edge is traversed at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n + m)

Space for adjacency list (m edges), memoization array (n), and recursion stack (up to n deep)

n
2n
โšก Linearithmic Space

Constraints

  • n == quiet.length
  • 1 โ‰ค n โ‰ค 500
  • 0 โ‰ค quiet[i] < n
  • All values of quiet are unique
  • 0 โ‰ค richer.length โ‰ค n * (n - 1) / 2
  • 0 โ‰ค ai, bi < n
  • ai != bi
  • All pairs (ai, bi) are unique
  • The observations in richer are all logically consistent
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen