아이디어 : DFS로 풀면 될 것 같다.
def dfs(visited, computers, i):
if visited[i] == True:
return 0
else:
visited[i] = True
for node in range(len(computers[i])):
if computers[i][node]==1:
dfs(visited, computers, node)
return 1
def solution(n, computers):
answer = 0
visited = [False] * n
for i in range(len(computers)):
answer+= dfs(visited, computers, i)
return answer
'✨ 공부 기록 > 알고리즘' 카테고리의 다른 글
[프로그래머스 lv 3] 단어 변환 (깊이/너비 우선 탐색(DFS/BFS)) (0) | 2025.02.28 |
---|---|
[프로그래머스 lv 2] 게임 맵 최단거리(깊이/너비 우선 탐색(DFS/BFS)) (0) | 2025.02.27 |
[프로그래머스 lv 2] 타겟 넘버(깊이/너비 우선 탐색(DFS/BFS)) (0) | 2025.02.26 |
[프로그래머스 lv 1] 같은 숫자는 싫어(코딩테스트 고득점 Kit/스택/큐) (0) | 2025.02.25 |
[프로그래머스 lv 0] 컨트롤 제트 (0) | 2025.02.25 |