How to Find the Town Judge using Graph Algorithm?

  • 时间:2020-09-30 16:23:25
  • 分类:网络文摘
  • 阅读:84 次

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: 2

Example 2:
Input: N = 3, trust = [[1,3],[2,3]]
Output: 3

Example 3:
Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1

Example 4:
Input: N = 3, trust = [[1,2],[2,3]]
Output: -1

Example 5:
Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3

Note:
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) —

推荐阅读:
5 Free Blogging Apps You Need On Your IPhone Right Now  The Exact Amount that You Must Spend on a Great Blog Is…  How to Make Videos Search Engines Love  5 Thoughts For Investing In Your Freelancing Career In 2017  How YouTube Can Work in Tandem With Your Blog  Essena O’Neill Quits Social Media  Kate Winslet Criticizes Social Media, Parents  Social Media Quizzes Could Put You At Risk For Hacking  Freelancing from Anywhere: Tips for Living and Working Abroad  5 Tools for Busy Bloggers 
评论列表
添加评论