[프로그래머스] 순위
[문제 링크]
https://programmers.co.kr/learn/courses/30/lessons/49191
[입출력 예]
n | results | return |
---|---|---|
5 | [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]] | 2 |
[소스코드]
def solution(n, results): win,lose = 1, -1 #1 # 결과를 숫자로 추상화 graph = [[0 for _ in range(n)] for _ in range(n)] #2 for result in results: winner, loser = result graph[winner-1][loser-1]=1 graph[loser-1][winner-1]=-1 # A가 B에게 이겼으면 graph[A][B]=1로 기록 # 졌으면 -1로 기록 for i in range(n): wins = [index for index, result in enumerate (graph[i]) if result==win] #3 # wins의 초기값은 i에게 진 사람들. # list comprehension # newlist = [expression for item in iterable if condition == True] while wins: loser =wins.pop() #4 # 패배자들 중 한 사람을 뽑아서 그 사람에게 진사람(index) 조회 for index, result in enumerate(graph[loser]): if result == win and graph[i][index]==0: graph[i][index], graph[index][i] = win, lose wins.append(index) return len(['count' for x in graph if x.count(0)==1]) #5 # 리스트 컴프리헨션을 응용한 리턴
'PS > Programmers' 카테고리의 다른 글
[프로그래머스] 셔틀버스 (0) | 2021.12.08 |
---|---|
[프로그래머스] 자물쇠와 열쇠 파이썬 (0) | 2021.11.28 |
[프로그래머스] 네트워크 파이썬 (0) | 2021.11.27 |
[프로그래머스] 정수삼각형 파이썬 풀이 (0) | 2021.11.27 |
[프로그래머스] 디스크 컨트롤러 파이썬 (0) | 2021.11.25 |
댓글
이 글 공유하기
다른 글
-
[프로그래머스] 셔틀버스
[프로그래머스] 셔틀버스
2021.12.08 -
[프로그래머스] 자물쇠와 열쇠 파이썬
[프로그래머스] 자물쇠와 열쇠 파이썬
2021.11.28 -
[프로그래머스] 네트워크 파이썬
[프로그래머스] 네트워크 파이썬
2021.11.27 -
[프로그래머스] 정수삼각형 파이썬 풀이
[프로그래머스] 정수삼각형 파이썬 풀이
2021.11.27
댓글을 사용할 수 없습니다.