In a group of N people (labelled 0, 1, 2, ..., N-1
), each person has different amounts of money, and different levels of quietness.html
For convenience, we'll call the person with label x
, simply "person x
".git
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.github
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
.ui
Example 1:code
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:htm
1 <= quiet.length = N <= 500
0 <= quiet[i] < N
, all quiet[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.richer
are all logically consistent.
這道題說是有N我的,給了咱們一個二維數組 richer,告訴了一些人之間的貧富關係,還有一個 quiet 數組,表示每一個人的安靜程度,對於每個人,讓找出最安靜,且不比其貧窮的人,注意這裏能夠包括本身。說實話,博主光開始沒有看懂這道題的例子,由於博主覺得返回的應該安靜值,其實返回的應該是人的編號纔對,這樣題目中的例子才能講得通,就好比說對於編號爲2的那人,richer 數組中只說明瞭其比編號爲1的人富有,但沒有顯示任何人比其富有,全部返回的人應該是其自己,因此 answer[2] = 2,還有就是編號爲7的人,這裏編號爲3,4,5,6的人都比起富有,可是其自己的安靜值最低,爲0,因此仍是返回其自己的編號,因此answer[7] = 7。blog
理解了題意以後就能夠開始作題了,這道題的本質其實就是有向圖的遍歷,LeetCode 中仍是有一些相似的題目的,好比 Course Schedule,Course Schedule II,Minimum 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
相似題目:
參考資料:
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