mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
e2b8a617d6
* add leetcode Find the Town Judge * updating DIRECTORY.md * Update leetcode/src/997.c Co-authored-by: David Leal <halfpacho@gmail.com> * updating DIRECTORY.md --------- Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
30 lines
824 B
C
30 lines
824 B
C
// Using hashtable.
|
|
// Runtime: O(n + len(trust))
|
|
// Space: O(n)
|
|
int findJudge(int n, int** trust, int trustSize, int* trustColSize){
|
|
int* personsToTrust = calloc(n + 1, sizeof(int));
|
|
int* personsFromTrust = calloc(n + 1, sizeof(int));
|
|
|
|
for(int i = 0; i < trustSize; i++){
|
|
int* currentTrust = trust[i];
|
|
personsToTrust[currentTrust[1]] += 1;
|
|
personsFromTrust[currentTrust[0]] += 1;
|
|
}
|
|
|
|
int potentialJudjeNumber = -1;
|
|
for(int i = 1; i < n + 1; i++){
|
|
if (personsToTrust[i] == n - 1 && personsFromTrust[i] == 0){
|
|
if (potentialJudjeNumber > -1){
|
|
return -1;
|
|
}
|
|
|
|
potentialJudjeNumber = i;
|
|
}
|
|
}
|
|
|
|
free(personsToTrust);
|
|
free(personsFromTrust);
|
|
|
|
return potentialJudjeNumber;
|
|
}
|