Skip to content
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
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Conversation

jee-woo
Copy link
Contributor

@jee-woo jee-woo commented Mar 29, 2023

No description provided.

@@ -0,0 +1,47 @@
// 인접 행렬을 1차원 배열로 만듦 -> 통과
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메모리 초과, 시간 초과를 1차원 배열을 이용해 해결할 수 있네요

let visited = new Array(n);
for (let i = 0; i < n; i++) visited[i] = new Array(n).fill(false);

const dfs = (i, j, height) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 각 방향마다 dfs 함수를 호출하는 방법도 있네요

visited[0] = true;

const adjacent = new Array(n + 1);
// adjacent 배열을 new Array(n + 1).fill(new Array(n + 1).fill(0))로 했을 경우 왜 밑에 forEach문이 제대로 동작하지 않는가?
Copy link
Contributor

@dongrri22 dongrri22 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반복문 안에서 자체적으로 new Array( ) 생성자를 반복해주면 어떨까요?


const adjacent = new Array(n + 1);
// adjacent 배열을 new Array(n + 1).fill(new Array(n + 1).fill(0))로 했을 경우 왜 밑에 forEach문이 제대로 동작하지 않는가?
// 한번에 여러 인덱스가 같이 바뀌어버리는 이유?
Copy link
Contributor

@dongrri22 dongrri22 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill 메서드가 값을 복사해 할당해 주는데, fill의 매개변수로 객체가 전달되어 동일한 주소가 전달되는게 아닐까요?

Copy link
Contributor

@dongrri22 dongrri22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이번주도 수고하셨습니다 :)

}
if (cabbageField[nowI]?.[nowJ + 1] && !visited[nowI]?.[nowJ + 1]) {
visited[nowI][nowJ + 1] = true;
queue.push([nowI, nowJ + 1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 반복문을 사용하면 더 간결할 것 같아요

3. queue 생성 (처음에 V 넣고 시작)
4. queue에 있는 노드를 빼고 해당 노드의 (방문하지 않은) 인접 노드들을 queue에 넣는다. 이때 queue에서 빠지면
visited 배열에 체크하고, 정답에 추가한다.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의사코드 적어놓는 것도 좋은 방법이네요!👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants