Leetcode

Leetcode 997. Find the Town Judg

2021-02-05  本文已影响0人  SnailTyan

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Find the Town Judge

2. Solution

class Solution:
    def findJudge(self, N, trust):
        if N == 1:
            return 1
        if len(trust) < N - 1:
            return -1
        judge = {}
        people = {}
        for pair in trust:
            people[pair[0]] = people.get(pair[0], 0) + 1
            judge[pair[1]] = judge.get(pair[1], 0) + 1

        for key, value in judge.items():
            if value == N - 1 and key not in people:
                return key

        return -1
class Solution:
    def findJudge(self, N, trust):
        count = [0] * (N + 1)
        for pair in trust:
            count[pair[0]] -= 1
            count[pair[1]] += 1
        
        for i in range(1, len(count)):
            if count[i] == N - 1:
                return i
        return -1

Reference

  1. https://leetcode.com/problems/find-the-town-judge/submissions/
上一篇 下一篇

猜你喜欢

热点阅读