-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloudandRich.cpp
More file actions
31 lines (29 loc) · 839 Bytes
/
loudandRich.cpp
File metadata and controls
31 lines (29 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Source: https://leetcode.com/problems/loud-and-rich/
// Author: Miao Zhang
// Date: 2021-03-18
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<vector<int>> graph(n);
for (auto& r: richer) {
graph[r[1]].push_back(r[0]);
}
vector<int> res(n, -1);
for (int i = 0; i < n; i++) {
dfs(i, res, graph, quiet);
}
return res;
}
private:
void dfs(int i, vector<int>& res, vector<vector<int>>& graph, vector<int>& quiet) {
if (res[i] != -1) return;
res[i] = i;
for (int j: graph[i]) {
dfs(j, res, graph, quiet);
if (quiet[res[j]] < quiet[res[i]]) {
res[i] = res[j];
}
}
}
};