반응형
https://school.programmers.co.kr/learn/courses/30/lessons/43162
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
import java.util.Stack;
class Solution {
boolean[] visited;
Stack<Integer> stack = new Stack<>();
public int solution(int n, int[][] computers) {
int answer = 0;
visited = new boolean[n];
for(int i=0; i<n; i++){
if(!visited[i]){
answer += 1;
stack.push(i);
dfs(n, computers);
}
}
return answer;
}
private void dfs(int n, int[][] computers){
if(stack.isEmpty()){
return;
}
int current = stack.pop();
visited[current] = true;
for(int i=0; i<n; i++){
if(computers[current][i]==1 && !visited[i]){
stack.push(i);
}
}
dfs(n, computers);
}
}반응형
'코딩테스트' 카테고리의 다른 글
| [프로그래머스/Java] 게임 맵 최단거리 (BFS) (0) | 2026.07.10 |
|---|---|
| BFS 공부 - 최단 거리 (최소 시간) (0) | 2026.07.10 |
| [프로그래머스/Java] 타겟 넘버 (DFS) (0) | 2026.07.09 |
| DFS 공부 (0) | 2026.07.09 |
| [프로그래머스/Java] 의상 (0) | 2026.07.09 |