Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created March 2, 2019 04:39
Show Gist options
  • Save fpdjsns/9fa94b57334915b92aa2587cd0246036 to your computer and use it in GitHub Desktop.
Save fpdjsns/9fa94b57334915b92aa2587cd0246036 to your computer and use it in GitHub Desktop.
[leetcode] 997. Find the Town Judge : https://leetcode.com/problems/find-the-town-judge/
class Solution {
public:
int findJudge(int N, vector<vector<int>>& trust) {
vector<int> cnt(N+1, 0);
for(int i=0;i<trust.size();i++){
int from = trust[i][0];
int to = trust[i][1];
cnt[to]++;
cnt[from] = -N; // The town judge trusts nobody.
}
int answer = -1;
for(int i=1;i<=N;i++){
if(cnt[i] == N-1){ // Everybody (except for the town judge) trusts the town judge.
if(answer == -1)
answer = i;
else // There is exactly one person that satisfies properties 1 and 2.
return -1;
}
}
return answer;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment