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.

这个题目的思路就是将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的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【PHP】快递鸟 物流查询接口实现

    官方网址: http://www.kdniao.com 即时查询api: http://www.kdniao.com/api-track 需要登录 ,申请一下 用户ID 和 API key 代码实现: ...

  2. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十一:PS/2模块⑤ — 扩展鼠标

    实验十一:PS/2模块⑤ — 扩展鼠标 当普通鼠标即三键鼠标再也无法满足需求的时候,扩展鼠标即滚轮鼠标就诞生了,然而实验十一的实验目的就是实现滚轮鼠标的驱动.不过,进入整体之前,先让我们来了解一下鼠标 ...

  3. Linux查看磁盘目录内存空间使用情况

    du 显示每个文件和目录的磁盘使用空间 命令参数 -c或--total  除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和. -s或--summarize  仅显示总计,只列出最后加总的 ...

  4. mysql 小于等于0 不包含null

    SELECT count(*) FROM test2 WHERE num<=1;

  5. MySQLCouldn't find MySQL manager

    mysql出现MySQLCouldn't find MySQL manager错误的解决办法 最近我的一个linux服务器上的网站全部无法打开.....访问网站没有提示.....只出现该页无法显示的错 ...

  6. Python之logging日志模块

    logging 用于便捷既然日志切线程安全的模块 vim log_test.py import logging logging.basicConfig(filename='log.log', form ...

  7. 洛谷P2564 生日礼物【单调队列】

    题目背景 四川2009NOI省选 题目描述 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可 ...

  8. SHU 413 - 添加好友

    题目链接:http://acmoj.shu.edu.cn/problem/413/ 不难发现,这题是求C(n,1)+C(n,2)+C(n,3)+……+C(n,n-1)+C(n,n) 根据二项展开式有( ...

  9. pycurl实例详解

    Pycurl是Python的libcurl接口.liburl是客户端的URL传输库,它支持FTP,FTPS,HTTP,HTTPS,TELNET,LDAP等诸多协议,同时支持HTTP认证,代理,FTP上 ...

  10. SVN版本控制系统搭建(结合http服务)

    SVN版本控制服务器搭建 Svn(subversion)是一个开源代码管理的控制系统,用来管理和存储开发的源代码,基于C/S模式.可以单独提供服务,也可以结合http服务来实现. 运行方式  运行端口 ...