In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and different levels of quietness.

For convenience, we'll call the person with label x, simply "person x".

We'll say that richer[i] = [x, y] if person x definitely has more money than person y.  Note that richer may only be a subset of valid observations.

Also, we'll say quiet[x] = q if person x has quietness q.

Now, return 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 person x.

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]
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 isn't 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.

Note:

  1. 1 <= quiet.length = N <= 500
  2. 0 <= quiet[i] < N, all quiet[i] are different.
  3. 0 <= richer.length <= N * (N-1) / 2
  4. 0 <= richer[i][j] < N
  5. richer[i][0] != richer[i][1]
  6. richer[i]'s are all different.
  7. The observations in richer are all logically consistent.

Runtime: 120 ms, faster than 8.56% of C++ online submissions for Loud and Rich.

class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<int> ret(n,);
for(int i=; i<n; i++) ret[i] = i;
vector<unordered_set<int>> graph(quiet.size());
vector<unordered_set<int>> revgraph(quiet.size());
for(int i=; i<richer.size(); i++){
graph[richer[i][]].insert(richer[i][]);
}
for(int i=; i<richer.size(); i++){
revgraph[richer[i][]].insert(richer[i][]);
}
queue<int> q;
for(int i=; i<n; i++){
if(revgraph[i].empty()) q.push(i);
}
while(!q.empty()){
int node = q.front(); q.pop();
for(auto it=graph[node].begin(); it!=graph[node].end(); it++){
int childnode = *it;
ret[childnode] = quiet[ret[node]] > quiet[ret[childnode]] ? ret[childnode] : ret[node];
revgraph[childnode].erase(node);
if(revgraph[childnode].empty()) q.push(childnode);
}
}
return ret;
}
};

还有一种dfs的做法,速度要快很多,因为没有用到字典删除。而且还有这一句话

auto __  = []() {std::ios::sync_with_stdio(false); return ;}();
auto __ =[]() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }();
void dfs(vector<vector<int>> & graph,vector<int> & go,vector<int> & retv,vector<int>quiets,int index)
{
int quiet = quiets[index];
go[index] = ;
retv[index] = index;
for(int i=;i<graph[index].size();i++)
{
if(go[graph[index][i]])
{
int person = retv[graph[index][i]];
if(quiet>quiets[person])
{
quiet = quiets[person];
retv[index] = person;
}
}
else
{
dfs(graph,go,retv,quiets,graph[index][i]);
int person = retv[graph[index][i]];
if(quiet>quiets[person])
{
quiet = quiets[person];
retv[index] = person;
}
}
}
}
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
vector<vector<int>> graph(quiet.size(),vector<int>());
if(quiet.size() == )
return vector<int>();
for(auto ele:richer)
{
graph[ele[]].push_back(ele[]);
}
vector<int> go(quiet.size(),);
vector<int> retv(quiet.size(),);
for(int i =;i<quiet.size();i++)
{
if(go[i]==)
{
dfs(graph,go,retv,quiet,i);
}
}
return retv;
}
};

LC 851. Loud and Rich的更多相关文章

  1. 851. Loud and Rich —— weekly contest 87

    851. Loud and Rich 题目链接:https://leetcode.com/problems/loud-and-rich/description/ 思路:有向图DFS,记录最小的quie ...

  2. 【LeetCode】851. Loud and Rich 解题报告(Python)

    [LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  3. [LeetCode] Loud and Rich 聒噪与富有

    In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...

  4. [Swift]LeetCode851. 喧闹和富有 | Loud and Rich

    In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...

  5. [LeetCode] 851. Loud and Rich_ Medium tag: DFS

    In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...

  6. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

  7. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

随机推荐

  1. Python之for循环与while循环

    for语句格式for x in range(起始值,结束值,步幅) 执行语句输出0,100各个数字for i in range(0,101) print(i)输出0,100的偶数for i in ra ...

  2. centos8/redhat8 无法上网,通过启动systemctl start NetworkManger搞定

    在systemd里面,可以直接使用systemctl进行管理 启动:systemctl start NetworkManger 关闭:systemctl stop NetworkManager 开机启 ...

  3. 1、Bash Shell

    一.什么是Bash shell BashShell是一个命令解释器,它在操作系统的最外层,负责用户程序与内核进行交互操作的一种接口,将用户输入的命令翻译给操作系统,并将处理后的结果输出至屏幕. 当我们 ...

  4. 异步通信&同步通信

    首先是两者的不同: 同步通信要求接收端时钟频率和发送端时钟频率一致,发送端发送连续的比特流:异步通信时不要求接收端时钟和发送端时钟同步,发送端发送完一个字节后,可经过任意长的时间间隔再发送下一个字节. ...

  5. zencart设置产品始终免运费sql

    zencart网站后台-Tools(工具)-Install SQL Patches(安装SQL脚本): 运行以下相应sql语句,即可实现产品始终免运费. zencart设置所有产品始终免运费: '; ...

  6. target runtime com.genuitec.runtime.genuitec.jee60 is not defined

    选中项目,右键 -> Properties -> Project Facets -> 在Runtimes 里 选择用Tomcat运行,然后 Apply -> OK. 问题解决.

  7. java 内部类(简单使用)

    什么是内部类 1.内部类是指在一个外部类的内部再定义一个类. 2.内部类作为外部类的一个成员,依附于外部类而存在. 3.内部类可为静态,可用protected和private修饰(而外部类只能使用pu ...

  8. thinkjs 安装笔记

    1.首先安装thinkjsnpm install -g thinkjs(-g是指全局安装)检查是否安装成功:thinkjs -v 2.创建项目进入项目目录,初始化项目:thinkjs new proj ...

  9. Hadoop-No.8之时间戳

    要获得良好的HBase的模式设计,要正确的理解和使用时间错.在HBase中,时间戳的作用如下所述. 时间戳决定了在put请求修改记录时那些记录更新 时间戳决定了一条记录的多个版本在返回时的排序 时间戳 ...

  10. Codeforces Round #451 (Div. 2) [ D. Alarm Clock ] [ E. Squares and not squares ] [ F. Restoring the Expression ]

    PROBLEM D. Alarm Clock 题 OvO http://codeforces.com/contest/898/problem/D codeforces 898d 解 从前往后枚举,放进 ...