feat: add Find the Town Judge LeetCode problem (#1199)

* 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>
This commit is contained in:
Alexander Pantyukhin 2023-01-31 23:26:17 +04:00 committed by GitHub
parent e8d3811f12
commit e2b8a617d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 170 additions and 140 deletions

View File

@ -113,6 +113,7 @@
| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree) | [C](./src/965.c) | Easy |
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array) | [C](./src/977.c) | Easy |
| 985 | [Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries) | [C](./src/985.c) | Medium |
| 997 | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge) | [C](./src/997.c) | Easy |
| 1008 | [Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal) | [C](./src/1008.c) | Medium |
| 1009 | [Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer) | [C](./src/1009.c) | Easy |
| 1019 | [Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list) | [C](./src/1019.c) | Medium |

29
leetcode/src/997.c Normal file
View File

@ -0,0 +1,29 @@
// 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;
}