Re: [閒聊] 每日LeetCode

作者: JerryChungYC (JerryChung)   2024-02-22 20:51:24
https://leetcode.com/problems/find-the-town-judge/
997. Find the Town Judge
小鎮上有n個人被標記為1到n,傳言其中有1個人是鎮法官
如果鎮法官存在:
1. 鎮法官不相信任何人
2. 除了鎮法官以外的每個人都信任鎮法官
3. 恰好有一個人滿足性質1和2
給一個信任數組,trust[i] = [ai, bi]表示標記為ai的人信任標記為bi的人
如果鎮法官存在,則傳回鎮法官的標籤,否則傳回-1
Example 1:
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Python3 code:

Links booklink

Contact Us: admin [ a t ] ucptt.com