알고리즘

[Lv_2] 도넛과 막대 그래프

빅디 2024. 1. 24. 02:55
728x90
from collections import defaultdict

def solution(edges):
    answer = [0, 0, 0, 0]
    graph = defaultdict(lambda: [0, 0])
    
    for s, e in edges:
        graph[s][1] += 1
        graph[e][0] += 1
        
    for items in graph.items():
        node, num = items
        
        if num[0] == 0 and num[1] >= 2:
            answer[0] = node
        elif num[0] >= 2 and num[1] >= 2:
            answer[3] += 1
        elif num[0] > 0 and num[1] == 0:
            answer[2] += 1
    
    # 시작정점에서 뻗어나간 간선의 개수 = 총 그래프 개수
    donut = graph[answer[0]][1] - answer[2] - answer[3]
    answer[1] = donut
    
    return answer

 

✔️ 링크: 코딩테스트 연습 - 도넛과 막대 그래프 | 프로그래머스 스쿨 (programmers.co.kr)