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 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 <= quiet.length = N <= 5000 <= quiet[i] < N, allquiet[i]are different.0 <= richer.length <= N * (N-1) / 20 <= richer[i][j] < Nricher[i][0] != richer[i][1]richer[i]'s are all different.- The observations in
richerare 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的更多相关文章
- 851. Loud and Rich —— weekly contest 87
851. Loud and Rich 题目链接:https://leetcode.com/problems/loud-and-rich/description/ 思路:有向图DFS,记录最小的quie ...
- 【LeetCode】851. Loud and Rich 解题报告(Python)
[LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- [LeetCode] Loud and Rich 聒噪与富有
In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...
- [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 ...
- [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 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
随机推荐
- win10双系统安装 linux(manjaro)记录
.clearFloat::after { content: ""; height: 0; display: block; clear: both; visibility: hidd ...
- IPC之ipc_sysctl.c源码解读
// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2007 * * Author: Eric Biederman <ebie ...
- STM32/MINI
- 在RecyclerView中集成QQ汽泡一
上次已经实现了QQ汽泡的自定义View的效果[http://www.cnblogs.com/webor2006/p/7726174.html],接着再将它应用到列表当中,这样才算得上跟QQ的效果匹配, ...
- sql 183. 从不订购的客户
SQL架构 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: +----+-------+ | Id | ...
- hashmap C++实现分析及std::unordered_map拓展
今天想到哈希函数,好像解决冲突的只了解了一种链地址法而且也很模糊,就查了些资料复习一下 1.哈希Hash 就是把任意长度的输入,通过哈希算法,变换成固定长度的输出(通常是整型),该输出就是哈希值. 这 ...
- 微服务框架SpringCloud与Dubbo
#v1.0.0# 1.背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给 ...
- JS 给数字加三位一逗号间隔的方法
1.方法 function format_number(n) { var b = parseInt(n).toString(); var len = b.length; ) { return b; } ...
- 如何判断元素是否在可视区域ViewPort
个性签名: 生如夏花,逝如冬雪:人生如此,何悔何怨. 前言: 经常需要计算元素的大小或者所在页面的位置,offsetWidth,clientWidth,scrollWidth,scrollTop这几个 ...
- docker安装rocketmq
一.单机部署 1.拉取镜像:foxiswho/rocketmq:server cabel/rocketmq:broker styletang/rocketmq-console-ng 2.创建目录:d ...