851. Loud and Rich
Question
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.
Example 1:
Input: richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3|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]
Constraints:
n == quiet.length1 <= n <= 5000 <= quiet[i] < n- All the values of
quietare unique. 0 <= richer.length <= n * (n - 1) / 20 <= ai, bi < nai != bi- All the pairs of
richerare unique. - The observations in
richerare all logically consistent.
Approach 1: Optimal
Intuition
- Edge
u -> v=uricher thanv. - For each person
i, we want the quietest person among allXthat can reachivia->(includingi). - Process people from richest to poorest (topo order).
- When a richer node knows its best quiet candidate, it pushes that candidate down to poorer nodes.
Algorithm
- Build directed graph
adj[richerPerson] -> poorerPerson. - Compute
indegreefor each node. - Initialize
ans[i] = i(best candidate for each is themselves). - Push all
indegree == 0nodes (richest) into a queue. - While queue not empty:
- Pop
current. - For each
neighborinadj[current]:- If
quiet[ans[current]] < quiet[ans[neighbor]], setans[neighbor] = ans[current]. - Decrement
indegree[neighbor]; if it hits 0, pushneighbor.
- If
- Pop
- Return
ans.
Code
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
// Build directed graph: richerPerson -> poorerPerson
vector<vector<int>> adj(n, vector<int>());
for (auto& edge: richer) {
int richerPerson = edge[0];
int poorerPerson = edge[1];
adj[richerPerson].push_back(poorerPerson);
}
// Compute indegree: number of richer people above each person
vector<int> indegree(n, 0);
for (int person = 0 ; person < n ; person++) {
for (int neighbor: adj[person]) {
indegree[neighbor]++;
}
}
// ans[i] = quietest person among all people richer-or-equal to i
// start by assuming each person is their own best candidate
vector<int> ans(n, 0);
for (int person = 0 ; person < n ; person++) {
ans[person] = person;
}
// Topological BFS starting from richest people (indegree 0)
queue<int> q;
for (int person = 0 ; person < n ; person++) {
if (indegree[person] == 0) {
q.push(person);
}
}
// Propagate quietest richer candidate along edges
while (!q.empty()) {
int current = q.front();
q.pop();
// current -> neighbor (neighbor is poorer)
for (int neighbor: adj[current]) {
// If the quietest richer-or-equal person for current
// is quieter than the quietest known for neighbor,
// update neighbor's best candidate
if (quiet[ans[current]] < quiet[ans[neighbor]]) {
ans[neighbor] = ans[current];
}
// Maintain topo order
indegree[neighbor]--;
if (indegree[neighbor] == 0) {
q.push(neighbor);
}
}
}
return ans;
}
};
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 2: Better
Intuition
Algorithm
Code
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 3: Optimal
Intuition
Algorithm
Code
Complexity Analysis
- Time Complexity:
- Space Complexity: