来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/loud-and-rich

题目描述

有一组 n 个人作为实验对象,从 0 到 n - 1 编号,其中每个人都有不同数目的钱,以及不同程度的安静值(quietness)。为了方便起见,我们将编号为 x 的人简称为 "person x "。

给你一个数组 richer ,其中 richer[i] = [ai, bi] 表示 person ai 比 person bi 更有钱。另给你一个整数数组 quiet ,其中 quiet[i] 是 person i 的安静值。richer 中所给出的数据 逻辑自恰(也就是说,在 person x 比 person y 更有钱的同时,不会出现 person y 比 person x 更有钱的情况 )。

现在,返回一个整数数组 answer 作为答案,其中 answer[x] = y 的前提是,在所有拥有的钱肯定不少于 person x 的人中,person y 是最安静的人(也就是安静值 quiet[y] 最小的人)。

示例 1:

输入:richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
输出:[5,5,2,5,4,5,6,7]
解释:
answer[0] = 5,
person 5 比 person 3 有更多的钱,person 3 比 person 1 有更多的钱,person 1 比 person 0 有更多的钱。
唯一较为安静(有较低的安静值 quiet[x])的人是 person 7,
但是目前还不清楚他是否比 person 0 更有钱。
answer[7] = 7,
在所有拥有的钱肯定不少于 person 7 的人中(这可能包括 person 3,4,5,6 以及 7),
最安静(有较低安静值 quiet[x])的人是 person 7。
其他的答案也可以用类似的推理来解释。

示例 2:

输入:richer = [], quiet = [0]
输出:[0]
 
提示:

n == quiet.length
1 <= n <= 500
0 <= quiet[i] < n
quiet 的所有值 互不相同
0 <= richer.length <= n * (n - 1) / 2
0 <= ai, bi < n
ai != bi
richer 中的所有数对 互不相同
对 richer 的观察在逻辑上是一致的

解题思路

这是一道标准的拓扑排序题

通过richer表,可以得到一张富有程度的拓扑表,通过拓扑排序的思路迭代就可以得出结果。

首先根据richer表构建一个关系表,每行记录比自己贫穷的人,并且记录比自己富有的人的人数。

从最富有的人开始依次向贫穷的人迭代,更新喧嚣值。

一个优化的点是可以用队列来记录当前最富有的值的人来避免每次从vector查找比自己富有的人的时间损耗。

源码展示

未优化前:

class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
vector<vector<int>> vviRelation(0);
vector<int> viRet;
vector<int> viCount;
vviRelation.resize(quiet.size());
viCount.resize(quiet.size(), 0);
for(int i = 0; i<quiet.size(); i++)
{
viRet.push_back(i);
}
for(auto person:richer)
{
vviRelation[person[0]].push_back(person[1]);
viCount[person[1]]++;
}
for(int i=0; i<quiet.size(); i++)
{
int iPos = find(viCount.begin(), viCount.end(), 0) - viCount.begin();
for(auto person :vviRelation[iPos])
{
if(quiet[iPos] < quiet[person])
{
viRet[person] = viRet[iPos];
quiet[person] = quiet[iPos];
}
viCount[person]--;
}
viCount[iPos]--;
}
return viRet;
}
};

优化后:

class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
vector<vector<int>> vviRelation(0);
vector<int> viRet;
vector<int> viCount;
vviRelation.resize(quiet.size());
viCount.resize(quiet.size(), 0);
queue<int> qiVisiting;
for(int i = 0; i<quiet.size(); i++)
{
viRet.push_back(i);
}
for(auto person:richer)
{
vviRelation[person[0]].push_back(person[1]);
viCount[person[1]]++;
}
for(int i=0; i<quiet.size(); i++)
{
if(viCount[i] == 0)
{
qiVisiting.push(i);
}
}
while(!qiVisiting.empty())
{
int iPos = qiVisiting.front();
qiVisiting.pop();
for(auto person :vviRelation[iPos])
{
if(quiet[viRet[iPos]] < quiet[viRet[person]])
{
viRet[person] = viRet[iPos];
}
viCount[person]--;
if(viCount[person] == 0)
{
qiVisiting.push(person);
}
}
}
return viRet;
}
};

运行结果

LeetCode-851 喧嚣与富有的更多相关文章

  1. [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 ...

  2. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  3. [LeetCode] questions conclustion_BFS, DFS

    BFS, DFS 的题目总结. Directed graph: Directed Graph Loop detection and if not have, path to print all pat ...

  4. C#LeetCode刷题-深度优先搜索

    深度优先搜索篇 # 题名 刷题 通过率 难度 98 验证二叉搜索树   22.2% 中等 99 恢复二叉搜索树   45.1% 困难 100 相同的树   48.1% 简单 101 对称二叉树   4 ...

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

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

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

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

  7. LeetCode最富有客户的资产总量

    最富有客户的资产总量 题目描述 给你一个 m * n 的整数网格 accounts,其中 account[i][j]是第 i 位客户在第 j 家银行托管的资产数量.返回最富有客户所拥有的资产总量. 客 ...

  8. 边工作边刷题:70天一遍leetcode: day 85-1

    Inorder Successor in BST 要点:这题要注意的是如果不是BST,没法从树结构上从root向那边找p,只能遍历.而根据BST,可以只走正确方向 如果不检查right子树,可以从ro ...

  9. Swift LeetCode 目录 | Catalog

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

  10. All LeetCode Questions List 题目汇总

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

随机推荐

  1. Ubuntu 22.04 搭建K8s集群

    目录 1. 虚拟机基础配置 配置静态ip 设置主机名 设置hosts 安装ssh 2. Ubuntu系统设置 禁用swap 修改内核参数 3. 安装containerd 4. 安装Kubernetes ...

  2. 4.1IDA基础设置--《恶意代码分析实战》

    1.加载一个可执行文件 ① 选项一:当加载一个文件(如PE文件),IDA像操作系统加载器一样将文件映射到内存中. ② 选项三:Binary File:将文件作为一个原始的二进制文件进行反汇编,例如文件 ...

  3. C#从实习到搬砖

    日常唠唠 没事就聊聊我在c#上踩过的那些坑,和一些笔记 少点比较,多些谦虚 会者不难 原博:轩先生大冒险 2022.4.19 datagridview 修改表头 dataGridView1.Colum ...

  4. RocketMQ Schema——让消息成为流动的结构化数据

    本文作者:许奕斌,阿里云智能高级研发工程师. Why we need schema RocketMQ 目前对于消息体没有任何数据格式的约束,可以是 JSON ,可以是对象 toString ,也可以只 ...

  5. Python + logging 控制台有日志输出,但日志文件中数据为空

    源码: def output(self, level, message): fh = logging.FileHandler(self.logpath, mode='a', encoding='utf ...

  6. SSM框架——MyBatis

    Mybatis 1.Mybatis的使用 1.1给项目导入相关依赖 我这里有几个下载好的依赖包提供给大家 点我下载--junit4.13.2 点我下载--maven3.8.1 点我下载--mybati ...

  7. [深度探索C++对象模型]trival constructor和non-trival constructor

    分清楚user-declared constructor和implict default constructor 首先要知道,如果你没有自定义一个类的构造函数,那么编译器会在暗中声明一个构造器,这个构 ...

  8. vue/cli子组件style中如何使用全局图片路径

  9. 动力节点—day06

    常用类 String String表示字符串类型,属于引用数据类型,不属于基本数据类型 在Java中随便使用双引号括起来的都是String对象,例如"abc"."def& ...

  10. linux命令与公私钥

    昨日内容回顾 etc目录 配置相关 /etc/profile 环境变量文件 /etc/motd 开机欢迎界面 usr目录 程序相关 四种安装软件的方式 1.yum安装 自动解决依赖性问题 2.rap安 ...