Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자료구조 트리
- 공부정리
- 카카오1차
- 프로그래머스
- java
- 객체프로그래밍
- 알고리즘
- 백준
- Java heap
- 카카오코테
- 객체프로그래밍이란
- 자바
- 문자열포맷
- 힙정렬자바
- 프렌즈4블록java
- 자료구조힙
- 개발상식
- 자바문자열
- 카카오기출
- 백준 1000번
- 백준 1924번 java
- 카카오코딩테스트
- heap정렬
- 프렌즈4블록
- 코테준비
- 백준 1924번
- 백준 1000번 java
- heap
- 코딩테스트기출
- java method
Archives
- Today
- Total
일단 시작해보는 블로그
[알고리즘_풀이] 프로그래머스 - 네트워크 java 본문
import java.util.*;
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
Node[] graph = new Node[n+1];
for(int i=1; i<=n; i++) {
graph[i] = new Node(i);
}
// 인접리스트 추가
for(int i=0; i<computers.length; i++) {
for(int j=0; j<computers[0].length; j++) {
if(i == j || computers[i][j] == 0) continue;
if(!graph[i+1].adjacent.contains(j+1)) {
graph[i+1].adjacent.add(j+1);
}
}
}
answer = dfs(graph, 1, n);
return answer;
}
public int dfs(Node[] graph, int begin, int n) {
int rtn = 0;
Stack<Integer> stack = new Stack<>();
for(int i=1; i<=n; i++) {
if(graph[i].checked == true) continue;
rtn++;
stack.add(i);
graph[i].checked = true;
while(!stack.isEmpty()) {
int out = stack.pop();
graph[out].checked = true;
for(int adj: graph[out].adjacent) {
if(graph[adj].checked == false) {
stack.add(adj);
}
}
}
}
return rtn;
}
class Node {
int key;
LinkedList<Integer> adjacent;
boolean checked;
Node(int key) {
this.key = key;
this.adjacent = new LinkedList<>();
this.checked = false;
}
}
}
'CS > 알고리즘 풀이' 카테고리의 다른 글
[알고리즘_풀이] 프로그래머스 - 최솟값 만들기 java (0) | 2020.03.11 |
---|---|
[알고리즘_풀이] 프로그래머스 - 숫자 야구 java (0) | 2020.03.11 |
[알고리즘_풀이] 프로그래머스 - 단어변환 java (0) | 2020.03.11 |
[알고리즘_풀이] 프로그래머스 등굣길 java (0) | 2020.03.06 |
[알고리즘_풀이] 프로그래머스 - 가장 먼 노드 java (0) | 2020.03.06 |
Comments