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.
這道題說是有N個人,給了我們一個二維數組 richer,告訴了一些人之間的貧富關系,還有一個 quiet 數組,表示每個人的安靜程度,對於每一個人,讓找出最安靜,且不比其貧窮的人,注意這里可以包括自己。說實話,博主光開始沒有看懂這道題的例子,因為博主以為返回的應該安靜值,其實返回的應該是人的編號才對,這樣題目中的例子才能講得通,就比如說對於編號為2的那人,richer 數組中只說明了其比編號為1的人富有,但沒有顯示任何人比其富有,所有返回的人應該是其本身,所以 answer[2] = 2,還有就是編號為7的人,這里編號為3,4,5,6的人都比起富有,但是其本身的安靜值最低,為0,所以還是返回其本身的編號,所以answer[7] = 7。
理解了題意之后就可以開始做題了,這道題的本質其實就是有向圖的遍歷,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