728x90
나의 풀이
def solution(n, results):
graph = [[0] * n for _ in range(n)]
rank = dict()
for w, l in results:
graph[w - 1][l - 1] = 1
graph[l - 1][w - 1] = -1
for i in range(n):
for j in range(n):
for k in range(n):
if graph[i][j] == graph[j][k] != 0:
graph[i][k] = graph[j][k]
graph[k][i] = graph[k][j]
for p in range(n):
if graph[p].count(0) == 1:
rank[p] = graph[p].count(1) + 1
return len(rank.keys())
defaultdict와 set을 활용한 풀이
from collections import defaultdict
def solution(n, results):
answer = 0
win, lose = defaultdict(set), defaultdict(set)
for result in results:
lose[result[1]].add(result[0])
win[result[0]].add(result[1])
for i in range(1, n + 1):
for winner in lose[i]: win[winner].update(win[i])
for loser in win[i]: lose[loser].update(lose[i])
for i in range(1, n+1):
if len(win[i]) + len(lose[i]) == n - 1: answer += 1
return answer
반응형
'Coding Test > Programmers' 카테고리의 다른 글
[프로그래머스/파이썬] 문자열 압축 (0) | 2022.06.09 |
---|---|
[프로그래머스/파이썬] 방의 개수 (0) | 2022.06.09 |
[프로그래머스/파이썬] 가장 먼 노드 (0) | 2022.06.09 |
[프로그래머스/파이썬] 징검다리 (0) | 2022.06.09 |
[프로그래머스/파이썬] 입국심사 (0) | 2022.06.08 |