-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
최주은 / 16주차 #23
Open
jee-woo
wants to merge
25
commits into
algorithm-tear-up-20b:main
Choose a base branch
from
jee-woo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
최주은 / 16주차 #23
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
5f01b7a
[BOJ] 예산 / 실버3 / 30분
jee-woo daf04d1
[BOJ] 나무 자르기 / 실버2 / 15분
jee-woo 48e89f2
[BOJ] 랜선 자르기 / 실버2 / 1시간
jee-woo 44a4272
IF문 좀 대신 써줘 / 실버3 / 1시간
jee-woo 5a5e007
용돈 관리 / 실버2 / 1시간
jee-woo 2f6ea9a
[BOJ] 안전 영역 / 실버1 / 20분
jee-woo 5ee1503
[BOJ] 유기농 배추 / 실버2 / 1시간
jee-woo 025d226
[BOJ] 연결 요소의 개수 / 실버2 / 1시간
jee-woo 65bf5ad
[BOJ] 경로 찾기 / 실버1 / 1시간
jee-woo e7101e5
[BOJ] DOM / 실버2 / 40분
jee-woo f657d4c
[BOJ] DFS와 BFS / 실버2 / 1시간
jee-woo 5478484
[BOJ] 유기농 배추 / 실버2 / 30분
jee-woo e7ca103
[BOJ] 촌수 계산 / 실버2 / 1시간
jee-woo 49217fb
[BOJ] 미로 탐색 / 실버1 / 30분~1시간
jee-woo f640652
[BOJ] 나이트의 이동 / 실버1 / 30분
jee-woo dd55f3d
[BOJ] 2xn 타일링 / 실버3 / 15분
jee-woo 5b9ddcd
[BOJ] 01타일 / 실버3 / 25분
jee-woo 536a159
[BOJ] 돌 게임 / 실버5 / 20분
jee-woo c0de78d
[BOJ] 이친수 / 실버3 / 20분
jee-woo c8830d4
[BOJ] 1로 만들기 / 실버3 / 20분
jee-woo 6473362
[BOJ] 진우의 달 여행 (Small) / 실버3 / 1시간 이내
jee-woo 9566e70
[BOJ] 연속합 / 실버2 / 30분
jee-woo 972914b
[BOJ] 부분수열의 합 / 실버2 / 30분 이상
jee-woo db5c204
[BOJ] 사탕 게임 / 실버2 / 1시간 이내 (레퍼런스 참고)
jee-woo dee1a5a
[BOJ] 숫자 야구 / 실버3 / 1시간 이상
jee-woo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
1. 인접 행렬 만들기 | ||
adj[i][j]: 노드 i에서 노드 j로 가는 간선이 있으면 1, 아니면 0 | ||
|
||
adj[1][2]: 1 | ||
adj[2][1]: 1 | ||
adj[2][5]: 1 | ||
adj[5][2]: 1 | ||
... | ||
|
||
2. visited 배열 만들기 | ||
모든 노드를 false로 초기화 한 후, dfs 실행할 때 방문한 노드는 true 할당하기 | ||
|
||
3. dfs 실행 | ||
count = 0으로 정하고, dfs 실행이 끝날 때마다 count++ | ||
for문 내에서 | ||
visited에 방문하지 않은 노드가 있을 경우 그 노드로 dfs 실행 | ||
|
||
for문이 끝나면 return count | ||
*/ | ||
|
||
// const input = require("fs") | ||
// .readFileSync("/dev/stdin") | ||
// .toString() | ||
// .trim() | ||
// .split("\n"); | ||
|
||
const input = ["6 5", "1 2", "2 5", "5 1", "3 4", "4 6"]; | ||
// const input = ["6 8", "1 2", "2 5", "5 1", "3 4", "4 6", "5 4", "2 4", "2 3"]; | ||
|
||
const [[N, M], edges] = [ | ||
input[0].split(" ").map(Number), | ||
input.slice(1).map((el) => el.split(" ").map(Number)), | ||
]; | ||
|
||
const solution = (n, edges) => { | ||
const visited = new Array(n + 1).fill(false); // node 개수만큼 | ||
visited[0] = true; | ||
|
||
const adjacent = new Array(n + 1); | ||
// adjacent 배열을 new Array(n + 1).fill(new Array(n + 1).fill(0))로 했을 경우 왜 밑에 forEach문이 제대로 동작하지 않는가? | ||
// 한번에 여러 인덱스가 같이 바뀌어버리는 이유? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fill 메서드가 값을 복사해 할당해 주는데, fill의 매개변수로 객체가 전달되어 동일한 주소가 전달되는게 아닐까요? |
||
|
||
for (let i = 0; i < adjacent.length; i++) { | ||
adjacent[i] = new Array(n + 1).fill(0); | ||
} | ||
|
||
edges.forEach((edge) => { | ||
adjacent[edge[0]][edge[1]] = 1; | ||
adjacent[edge[1]][edge[0]] = 1; | ||
}); | ||
|
||
const dfs = (node) => { | ||
visited[node] = true; | ||
for (let i = 1; i < adjacent[node].length; i++) { | ||
if (adjacent[node][i] === 1 && !visited[i]) dfs(i); | ||
} | ||
return; | ||
}; | ||
|
||
let count = 0; | ||
for (let i = 1; i <= n; i++) { | ||
if (!visited[i]) { | ||
dfs(i); | ||
count++; | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
console.log(solution(N, edges)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반복문 안에서 자체적으로 new Array( ) 생성자를 반복해주면 어떨까요?