Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
loud-and-rich.cpp 1003 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-06-10 15:23 +08:00 . Update loud-and-rich.cpp
// Time: O(q + r)
// Space: O(q + r)
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
vector<vector<int>> graph(quiet.size());
for (const auto& r : richer) {
graph[r[1]].emplace_back(r[0]);
}
vector<int> result(quiet.size(), -1);
for (int i = 0; i < quiet.size(); ++i) {
dfs(graph, quiet, i, &result);
}
return result;
}
private:
int dfs(const vector<vector<int>>& graph,
const vector<int>& quiet,
int node, vector<int> *result) {
if ((*result)[node] == -1) {
(*result)[node] = node;
for (const auto& nei : graph[node]) {
int smallest_person = dfs(graph, quiet, nei, result);
if (quiet[smallest_person] < quiet[(*result)[node]]) {
(*result)[node] = smallest_person;
}
}
}
return (*result)[node];
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助