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



这道题说是有N我的,给了咱们一个二维数组 richer,告诉了一些人之间的贫富关系,还有一个 quiet 数组,表示每一个人的安静程度,对于每个人,让找出最安静,且不比其贫穷的人,注意这里能够包括本身。说实话,博主光开始没有看懂这道题的例子,由于博主觉得返回的应该安静值,其实返回的应该是人的编号才对,这样题目中的例子才能讲得通,就好比说对于编号为2的那人,richer 数组中只说明了其比编号为1的人富有,但没有显示任何人比其富有,全部返回的人应该是其自己,因此 answer[2] = 2,还有就是编号为7的人,这里编号为3,4,5,6的人都比起富有,可是其自己的安静值最低,为0,因此仍是返回其自己的编号,因此answer[7] = 7。blog

理解了题意以后就能够开始作题了,这道题的本质其实就是有向图的遍历,LeetCode 中仍是有一些相似的题目的,好比 Course ScheduleCourse Schedule IIMinimum 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



相似题目:

Course Schedule

Course Schedule II

Minimum Height Trees

Reconstruct Itinerary



参考资料:

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


LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索