그냥하는코딩
프로그래머스 3단계 - [네트워크] 본문
https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
1. 문제이해
- BFS 를 활용한 전체 탐색
- 연결된 묶음 갯수세기
2. 코드 작성
def solution(n, computers):
answer = 0
dict_computer = dict()
temp = set()
for i, computer in enumerate(computers):
dict_computer[i] = [idx for idx, c in enumerate(computer) if c == 1 if idx != i]
for k in dict_computer.keys():
if k not in temp:
temp.add(k)
temp.update(set(bfs(dict_computer, k)))
answer += 1
return answer
def bfs(graph, start_node):
visited = []
need_visit = [start_node]
while need_visit:
node = need_visit.pop(0)
if node not in visited:
visited.append(node)
need_visit.extend(graph[node])
return visited
※ 시간복잡도
O((V+E)*V) : V(노드의 수), E(연결의 수)
3. 풀이간 체크사항
1) BFS의 가장 기본적인 문제라고 생각된다.
'부스트캠프 준비기 > 코딩테스트 알고리즘' 카테고리의 다른 글
| 프로그래머스 2단계 - [괄호 회전하기] (0) | 2021.06.21 |
|---|---|
| [구글 Colaboratory] 을 활용한 알고리즘 정리 (0) | 2021.06.21 |
| 프로그래머스 1단계 - [행렬의 덧셈] (0) | 2021.06.05 |
| 파이썬 언어 코딩테스트 용 함수 알고리즘 정리 (0) | 2021.05.23 |
| 프로그래머스 1단계 - [1차 비밀지도] (0) | 2021.05.21 |
Comments