Re: [閒聊] 每日LeetCode已回收
看板Marginalman作者JerryChungYC (JerryChung)時間7月前 (2024/02/22 20:51)推噓0(0推 0噓 0→)留言0則, 0人參與討論串707/719 (看更多)
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:
------------------------------------------------------------------
class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
if not trust: return 1 if n == 1 else -1
res = Counter(_[1] for _ in trust).most_common(1)[0]
return res[0] if len(set(_[0] for _ in trust)) == n - 1 and res[1] == n - 1 else -1
------------------------------------------------------------------
res是統計被信任的次數,然後為次數最多的 [標籤,次數]
因為所有人都信任鎮法官,所以a的人數應該要是n-1
而同時res[1]的數量也要是n-1
不知道自己在寫什麼 不過過了就 對啊
又順利水過一天囉
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.54.65 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1708606286.A.72B.html
討論串 (同標題文章)
完整討論串 (本文為第 707 之 719 篇):
閒聊
1
3