forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindTheTownJudge.java
42 lines (38 loc) · 913 Bytes
/
FindTheTownJudge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
// TC : O(n)
// SC : O(n)
public int findJudge(int N, int[][] trust) {
int[] inwardDegree = new int[N+1];
int[] outwardDegree = new int[N+1];
for(int i=0;i<trust.length;i++){
int countOut = outwardDegree[trust[i][0]];
outwardDegree[trust[i][0]] = countOut+1;
int countIn = inwardDegree[trust[i][1]];
inwardDegree[trust[i][1]]= countIn+1;
}
for(int i=1;i<=N;i++){
if(outwardDegree[i]==0&& inwardDegree[i]==N-1){
return i; // town judge
}
}
return -1;
}
}
//C++ CODE
class Solution {
public:
int findJudge(int n, vector<vector<int>>& trust) {
vector<int>in(n+1,0);
for(int i=0;i<trust.size();i++){
in[trust[i][1]]++;
in[trust[i][0]]--;
}
int ans = -1;
for(int i=1;i<n+1;i++){
if(in[i] == n-1){
ans = i;
}
}
return ans;
}
};