↓Skip to main content
  1. LeetCode/

LeetCode 997: Find the Town Judge

·2 mins· ·
LeetCode Easy Array Hash-Table Graph
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-27

  • Submissions: Runtime Error → Wrong Answer → Runtime Error → Wrong Answer ×2 → Runtime Error → Wrong Answer ×3 → Runtime Error ×2 → Wrong Answer ×4 → Accepted ×2 → Runtime Error → Wrong Answer ×2 → Accepted

Approach
#

Treat trust as a directed graph. The judge is trusted by everyone else (in-degree n - 1) and trusts nobody (out-degree 0). Track in - out in one array; the person reaching n - 1 is the judge (only the judge can, since in-degree is at most n - 1). With n = 1 and no trust at all, that single person is the judge.

My Attempts
#

This took 21 submissions across 2022-09 and 2022-11:

  • At first I guessed: return False when n == len(trust), or just return n, so n = 2 with no trust returned 2 instead of -1.
  • Counting in-degrees, I checked only “trusted by n - 1 people” without checking that the judge trusts nobody; after adding that, n = 1 with empty trust failed (answer 1, returned -1), and special-casing trust == [] to return 1 broke n = 2.
  • The 2022-11 rewrite separated the special cases (n == 1 returns 1, otherwise -1) and passed. Using in - out == n - 1 needs none of them.

Solution
#

class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -> int:
        score = [0] * (n + 1)
        for a, b in trust:
            score[a] -= 1
            score[b] += 1
        for i in range(1, n + 1):
            if score[i] == n - 1:
                return i
        return -1

Complexity
#

Time O(n + t), space O(n).

Mistakes to Avoid
#

  • Both conditions apply: trusted by n - 1 people and trusting no one.
  • With n = 1 and empty trust the answer is 1.

Interview Explanation
#

The judge is trusted by all others and trusts nobody, so I record in-degree minus out-degree per person and return whoever reaches n - 1. Time O(n + t).

Related

LeetCode 1002: Find Common Characters
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 1002 note: count each word and intersect all Counters (minimum count per character), then expand
LeetCode 929: Unique Email Addresses
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 929 note: split at @, normalize only the local part (drop everything after +, remove dots), rejoin with the domain and collect in a set
LeetCode 953: Verifying an Alien Dictionary
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 953 note: map letters to their alien index and compare each adjacent pair at the first differing character; with no difference, the shorter must come first
LeetCode 914: X of a Kind in a Deck of Cards
·2 mins
LeetCode Easy Array Hash-Table Math Counting
LeetCode 914 note: every card count must be divisible by one X >= 2, i.e. the GCD of all counts is at least 2
LeetCode 888: Fair Candy Swap
·2 mins
LeetCode Easy Array Hash-Table Binary-Search Sorting
LeetCode 888 note: equal sums after swapping x and y means y = x + (sumB - sumA) / 2; put B in a set and check each x from A
LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned