[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 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 <= 500
0 <= quiet[i] < N
, allquiet[i]
are different.0 <= richer.length <= N * (N-1) / 2
0 <= richer[i][j] < N
richer[i][0] != richer[i][1]
richer[i]
's are all different.- The observations in
richer
are all logically consistent.
这个题目的思路就是将richer转换为graph, 然后是由穷的往富的走, 因为穷的subtree更多, 所以可以直接利用富的quietest, 然后跟自身进行比较即可, 只不过除了graph之外还有一个quiet, 实际上就像一个dictionary of value.
1. Constraints
1) quite.length = [1,500], so not empty
2) 0 <= quiet[i] < N
, all quiet[i]
are different., no duplicates
3) richer and element will be valiad and no duplicates, all logivally consistent, no loop in the graph
2. Ideas
DFS : T: O(n) S: O(n)
1) 得到N, 初始化ans
2)将richer 转换为graph
3) 利用dfs, 只不过利用post order的顺序, 可以利用ans来cache 之前的结果, 确实很巧妙
4) for loop 将所有点都scan一遍, 返回最后的ans
3. Code
class Solution:
def loudRich(self, richer, quiet):
N, graph = len(quiet), collections.defaultdict(set)
ans = [None]*N
for x, y in richer:
graph[y].add(x)
def dfs(i):
if ans[i] == None:
ans[i] = i
for each in graph[i]:
cand = dfs(each)
if quiet[cand] < quiet[ans[i]]:
ans[i] = cand
return ans[i]
for i in range(N):
dfs(i)
return ans
4. test case
richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
[LeetCode] 851. Loud and Rich_ Medium tag: DFS的更多相关文章
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [LeetCode] 200. Number of Islands_ Medium tag: BFS
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
随机推荐
- vim 正则替换【转】
:[range]s/from/to/[flags] range:搜索范围,如果没有指定范围,则作用于但前行. :1,10s/from/to/ 表示在第1到第10行(包含第1,第10行)之间搜索替换: ...
- Esper学习之十五:Pattern(二)
上一篇开始了新一轮语法——Pattern的讲解,一开始为大家普及了几个基础知识,其中有说到操作符.当时只是把它们都列举出来了,所以今天这篇就是专门详解这些操作符的,但是由于篇幅限制,本篇先会讲几个,剩 ...
- OpenStack入门之【OpenStack-havana】之单网卡-All In One 安装(基于CentOS6.4)
这篇文章是自己的一篇老文,分享下,请君慢用.... =========================================== [特别申明]:经过了一段时间的不断学习加不断的测试得出本文, ...
- linux netcat命令使用技巧
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...
- mongodb gridfs基本使用
Mongodb GridFS图片文件存储解决方案 之前解决方案是接收图片数据后,将图片直接存储到盘阵,然后通过Apache做服务器,将图片信息存储到数据库,并且存储一个Apache的访问路径. 目前需 ...
- 【CF917D】Stranger Trees 树形DP+Prufer序列
[CF917D]Stranger Trees 题意:给你一棵n个点的树,对于k=1...n,问你有多少有标号的n个点的树,与给出的树有恰好k条边相同? $n\le 100$ 题解:我们先考虑容斥,求出 ...
- you do not have permission to pull from the repository解决方法
使用git进行项目的版本管理,换了台电脑,配置了账号和邮箱后,pull一个私有项目的时候,发现一个问题: 原因分析: 这是由于没有设置Gitee的SSH公钥.在未设置SSH公钥的情况下,可以使用git ...
- Django---Mysql数据库链接
Django链接Mysql数据库: 第一步:创建应用 python manage.py startapp index 第二步:将应用添加到配置里面 settings INSTALLED_APPS = ...
- Ubuntu16.04 安装lamp环境
拿到新装的ubuntu16.04新系统 首先 apt-get update 更新一下 我这里是root用户,如果您不是超级管理员,命令前加sudo即可 如果您加了sudo也不好使,那就联系管理员,给你 ...
- HDU 2829 - Lawrence - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 T. E. Lawrence was a controversial figure during ...