파이썬 알고리즘 : 전력망을 둘로 나누기, 쿼드 압축 후 개수 세기, 합승 택시 요금
2023년 8월 26일 알고리즘 문제풀이 문제 1 프로그래머스 전력망을 둘로 나누기 문제 링크 1차 시도 나의 생각 한 간선이 없어질 때마다 값을 모두 구한 후 가장 절대값이 작을 때를 찾았다. 결과 정답 코드 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 from collections import deque def solution(n, wires): answer = n d = len(wires) for i in range(d): arr = deque() arr.append(0) graph = [[] for _ in range(n)] visited = [False for _ in range(n)] visited[0] = True for j in range(d): if j == i: continue else: a,b = wires[j] graph[a-1].append(b-1) graph[b-1].append(a-1) while arr: now = arr.popleft() for x in graph[now]: if not visited[x]: visited[x] = True arr.append(x) cnt = visited.count(True) cnt = abs(n-2*cnt) answer = min(answer,cnt) return answer 문제 2 프로그래머스 쿼드압축 후 개수 세기 문제 링크 ...