How to Find the Town Judge using Graph Algorithm?
- 时间:2020-09-30 16:23:25
- 分类:网络文摘
- 阅读:111 次
In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
The town judge trusts nobody.
Everybody (except for the town judge) trusts the town judge.
There is exactly one person that satisfies properties 1 and 2.
You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
Example 1:
Input: N = 2, trust = [[1,2]]
Output: 2Example 2:
Input: N = 3, trust = [[1,3],[2,3]]
Output: 3Example 3:
Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1Example 4:
Input: N = 3, trust = [[1,2],[2,3]]
Output: -1Example 5:
Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3Note:
1 <= N <= 1000
trust.length <= 10000
trust[i] are all different
trust[i][0] != trust[i][1]
1 <= trust[i][0], trust[i][1] <= N
C++ Implementation to Find Town Judge using Graph Algorithm
Let’s use two arrays to count the number of incoming and outgoing edges (trust). For outgoing edges from A to B, we immediately invalidate A being the town judge according to the problem statement.
For incoming edges A to B, we increment the counter count[B]. When we know there are N-1 incoming connections, we make a note on the B. However, if there is a second candidate that has received N-1 connections, we know there are no answers which we can simply return -1.
One corner case is when there is only 1 people and the trust array is empty, the people himself/herself is the judge although he trusts nobody.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution { public: int findJudge(int N, vector<vector<int>>& trust) { vector<int> trust_count(N, 0); vector<bool> valid(N, true); int c = 0, x = -1; for (const auto &n: trust) { trust_count[n[1] - 1] ++; if (trust_count[n[1] - 1] == N - 1) { // receive N-1 incoming edges c ++; x = n[1]; if (c >= 2) return -1; // more than 1 satisfied, thus no judges } valid[n[0] - 1] = false; // the judge trusts nobody } if (c == 0) return N == 1 ? 1 : -1; // corner case if (valid[x - 1]) return x; // c = 1, also judge trusts nobody. return -1; } }; |
class Solution {
public:
int findJudge(int N, vector<vector<int>>& trust) {
vector<int> trust_count(N, 0);
vector<bool> valid(N, true);
int c = 0, x = -1;
for (const auto &n: trust) {
trust_count[n[1] - 1] ++;
if (trust_count[n[1] - 1] == N - 1) { // receive N-1 incoming edges
c ++;
x = n[1];
if (c >= 2) return -1; // more than 1 satisfied, thus no judges
}
valid[n[0] - 1] = false; // the judge trusts nobody
}
if (c == 0) return N == 1 ? 1 : -1; // corner case
if (valid[x - 1]) return x; // c = 1, also judge trusts nobody.
return -1;
}
};The above C++ implementation runs at O(N) time and space complexity.
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:大暑养生:暑天应多吃清淡易消化食物 市场热俏的功能性饮料到底是什么? 不能把功能性饮料完全代替饮用水 功能性饮料应该如何科学合理的饮用 网传的10种致癌食物中有9种不靠谱 夏季常见水果:西瓜的营养保健价值 健康食品黑枣的食疗功效与营养价值 保健食品当做药品卖“脑力风暴”骗局 “公益网站”牵线 保健食品当药品卖 消费者如何正确选择购买保健食品
- 评论列表
-
- 添加评论