-
Notifications
You must be signed in to change notification settings - Fork 859
Expand file tree
/
Copy pathProblem-1
More file actions
23 lines (20 loc) · 710 Bytes
/
Problem-1
File metadata and controls
23 lines (20 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Time Complexity: O(m + n), m -> no. of trust relations, n -> no. of people
// Space Complexity: O(n)
// Keep track of the number of trusts for each person in an array
// If a person trusts someone decrement the count in array, increment when someone trusts that person
// Return the person with n count, else return -1
class Solution {
public int findJudge(int n, int[][] trust) {
int[] arr = new int[n+1];
for(int i = 0; i < trust.length; i++) {
arr[trust[i][0]]--;
arr[trust[i][1]]++;
}
// Check for town judge
for(int i = 1; i < n+1; i++) {
if(arr[i] == n-1)
return i;
}
return -1;
}
}