997. Find the Town Judge

Doyeon Kim·2023년 1월 23일

코딩테스트 공부

목록 보기
158/171

https://leetcode.com/problems/find-the-town-judge/description/


마을의 판사를 찾는 문제이다

주어진 조건
1.판사는 아무도 신뢰하지 않음
2.판사는 모든 마을 사람의 신뢰를 받음

신뢰하는 쪽은 빼주고 신뢰받는 쪽은 더해주어 마을 사람의 수와 같아지면 답을 반환한다

class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -> int:
        t= [0]*(n+1)

        for a,b in trust:
            t[a]-=1
            t[b]+=1

        for i in range(1,len(t)):
            if t[i]==n-1:
                return i
        return -1
profile
성장하고 도전하는 개발자. 프로그래밍 좋아하세요?

0개의 댓글