소프트웨어에 대한 모든 것

LeetCode 풀기 - 997. Find the Town Judge 본문

알고리즘/LeetCode

LeetCode 풀기 - 997. Find the Town Judge

앤테바 2022. 1. 4. 21:23
반응형

997. Find the Town Judge

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

 

Find the Town Judge - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

문제 설명:

  • 1) 타운 판사는 아무도 믿지 않는다
  • 2) 타운 판사를 제외하고 모든 사람은 타운 판사를 믿지 않는다
  • 1)과 2) 조건을 만족하는 사람은 딱 한 명 있다

 

class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -> int:
        # n이 1부터 시작하므로 n+1 만큼 크기 생성
        points = [0] * (n+1)
        
        for t in trust:
            # 다른 사람을 믿으면 -1점
            points[t[0]] -= 1
            
            # 다름 사람으로 부터 믿음을 얻으면 +1 점
            points[t[1]] += 1
        
        for i in range(1, n+1):
            # 자신을 제외한 n-1 명에게 믿음을 얻은 사람이 town judge
            if points[i] == n-1:
                return i
        
        # not exist town judge
        return -1

 

반응형
Comments