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.

这道题说是有N个人,给了我们一个二维数组 richer,告诉了一些人之间的贫富关系,还有一个 quiet 数组,表示每个人的安静程度,对于每一个人,让找出最安静,且不比其贫穷的人,注意这里可以包括自己。说实话,博主光开始没有看懂这道题的例子,因为博主以为返回的应该安静值,其实返回的应该是人的编号才对,这样题目中的例子才能讲得通,就比如说对于编号为2的那人,richer 数组中只说明了其比编号为1的人富有,但没有显示任何人比其富有,所有返回的人应该是其本身,所以 answer[2] = 2,还有就是编号为7的人,这里编号为3,4,5,6的人都比起富有,但是其本身的安静值最低,为0,所以还是返回其本身的编号,所以answer[7] = 7。

理解了题意之后就可以开始做题了,这道题的本质其实就是有向图的遍历,LeetCode 中还是有一些类似的题目的,比如 Course ScheduleCourse Schedule IIMinimum Height Trees,和 Reconstruct Itinerary 等。对于这类的题,其实解法都差不多,首先都是需要建立图的结构,一般都是用邻接链表来表示,在博主之前的那篇博客 Possible Bipartition,分别使用了二维数组和 HashMap 来建立邻接链表,一般来说,使用 HashMap 能节省一些空间,且更加灵活一些,所以这里还是用 HashMap 来建立。由于要找的人必须等于或者富于自己,所以我们可以建立每个人和其所有富于自己的人的集合,因为这里除了自己,不可能有人和你一样富的人。建立好图的结构后,我们可以对每个人进行分别查找了,首先每个人的目标安静值可以初始化为自身,因为就算没有比你富有的人,你自己也可以算满足条件的人,从 HashMap 中可以直接查找到比你富有的人,他们的安静值是可以用来更新的,但是还可能有人比他们还富有,那些更富有的人的安静值也得查一遍,所以就需要用递归来做,遍历到每个比你富有的人,对他再调用递归,这样返回的就是比他富有或相等,且安静值最小的人,用这个安静值来更新当前人的安静值即可,注意我们在递归的开始首先要查一下,若某人的安静值已经更新了,直接返回即可,不用再重复计算了,参见代码如下:

解法一:

class Solution {
public:
vector<int> loudAndRich\(vector<vector<int>>& richer, vector<int>& quiet) {
vector<int> res(quiet.size(), -1);
unordered_map<int, vector<int>> findRicher;
for (auto a : richer) findRicher[a[1]].push_back(a[0]);
for (int i = 0; i < quiet.size(); ++i) {
helper(findRicher, quiet, i, res);
}
return res;
}
int helper(unordered_map<int, vector<int>>& findRicher, vector<int>& quiet, int i, vector<int>& res) {
if (res[i] > 0) return res[i];
res[i] = i;
for (int j : findRicher[i]) {
if (quiet[res[i]] > quiet[helper(findRicher, quiet, j, res)]) {
res[i] = res[js];
}
}
return res[i];
}
};

我们也可以使用迭代的写法,这里还是要用邻接链表来建立图的结构,但是有所不同的是,这里需要建立的映射是每个人跟所有比他穷的人的集合。然后还有建立每个人的入度,将所有入度为0的人将入队列 queue,先开始遍历,入度为0,表示是已经条件中没有人比他更富,那么就可以通过 HashMap 来遍历所有比他穷的人,若当前的人的安静值小于比他穷的人的安静值,那么更新比他穷的人的安静值,可以看到这里每次更新的都是别人的安静值,而上面递归的解法更新的都是当前人的安静值。然后将比他穷的人的入度减1,当减到0时,加入到队列 queue 中继续遍历,参见代码如下:

解法二:

class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<int> res(n, -1), inDegree(n);
unordered_map<int, vector<int>> findPoorer;
queue<int> q;
for (auto a : richer) {
findPoorer[a[0]].push_back(a[1]);
++inDegree[a[1]];
}
for (int i = 0; i < quiet.size(); ++i) {
if (inDegree[i] == 0) q.push(i);
res[i] = i;
}
while (!q.empty()) {
int cur = q.front(); q.pop();
for (int next : findPoorer[cur]) {
if (quiet[res[next]] > quiet[res[cur]]) res[next] = res[cur];
if (--inDegree[next] == 0) q.push(next);
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/851

类似题目:

Course Schedule

Course Schedule II

Minimum Height Trees

Reconstruct Itinerary

参考资料:

https://leetcode.com/problems/loud-and-rich/

https://leetcode.com/problems/loud-and-rich/discuss/137918/C%2B%2BJavaPython-Concise-DFS

https://leetcode.com/problems/loud-and-rich/discuss/138088/C%2B%2B-with-topological-sorting

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Loud and Rich 聒噪与富有的更多相关文章

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

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

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

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

  3. LC 851. 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. Swift LeetCode 目录 | Catalog

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

  6. All LeetCode Questions List 题目汇总

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

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

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

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

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

  9. iOS 非ARC基本内存管理系列 2-多对象内存管理(3) 利用@property来自动管理内存

    iOS 基本内存管理-多对象内存管理(2)中可以看到涉及到对象的引用都要手动管理内存:每个对象都需要写如下代码 // 1.对要传入的"新车"对象car和目前Person类对象所拥有 ...

随机推荐

  1. Spring Boot系列(二) Spring Boot 之 REST

    Rest (Representational Stat Transer) 是一种软件架构风格. 基础理论 架构特性 性能 可伸缩 简化的统一接口 按需修改 组件通信透明 可移植 可靠性 架构约束 C/ ...

  2. LeetCode第十九题-链表节点的删除

    Remove Nth Node From End of List 问题简介;给定链表,从链表末尾删除第n个节点并返回其头部 例: 给定链表:1-> 2-> 3-> 4-> 5, ...

  3. hihocoder 1505

    hihocoder 1505 题意:给你n个数,让你从n个数中抽两个数,再抽两个数,使得前两个数和后两个数相等 分析:对 i,j,p,q遍历的话时间复杂度会达到o(n4),所以考虑优化p,q 假设分配 ...

  4. 题解 P4705 【玩游戏】

    这题是真的神仙啊...居然用的 stl 来卡常? 话说 998244353 真的可以一眼 NTT ? noteskey 所以说只要推柿子就好了但是有的地方的推导根本就想不到... 我们令第 t 个答案 ...

  5. ansible-plabybook 常用的有用的命令

    ansible-plabybook 常用的有用的命令 ansible-playbook常用的非常有用的参数有: -C ,大写c ,这个命令的意思就是模拟执行,会告诉你跑完这个playbook会发生什么 ...

  6. vue的过滤器filter

    前记: 排版记录,未免太耽误时间,我就简单的来,下面是一个vue 过滤器的写法,demo 演示,限制一个字符串的长度. vue filter 的官网介绍 https://cn.vuejs.org/v2 ...

  7. 有名的素MM

    from math import sqrt item=[] for yr in [1988,1989]: for mth in range(1,13): if mth in [1,3,5,7,8,10 ...

  8. 无向图 解决Unity地图上固定网络上,标记走固定步数能到达的位置

    首先需要了解无向图的定义 参考:https://www.cnblogs.com/wxgblogs/p/5572391.html 我们选择链表的方式进行操作. int StartPositon; int ...

  9. 让oracle数据库的表的id自动递增

    1.创建递增序列 CREATE SEQUENCE  ID_ADD(序列名称)INCREMENT BY 1START WITH 1MINVALUE 1NOMAXVALUE; 2.创建触发器绑定到表上cr ...

  10. 五 Zabbix全网监控

    监控的作用 我们的职责   1.保障企业数据的安全可靠.   2.为客户提供7*24小时服务.   3.不断提升用户的体验.在关键时刻,提前提醒我们服务器要出问题了当出问题之后,可以便于找到问题的根源 ...