-
-
Notifications
You must be signed in to change notification settings - Fork 195
[uraflower] Week11 Solutions #1570
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
base: main
Are you sure you want to change the base?
Conversation
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.
복잡도 계산이나 풀이 설명을 깔끔하게 작성해주셔서 공부가 많이 되었습니다!
이번 한 주도 고생 많으셨어요~! 🍀
const queue = [[-1, 0]]; // 부모 노드, 자식 노드 | ||
const visited = new Set(); | ||
|
||
while (queue.length) { |
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.
무방향 그래프가 트리의 조건인 connected && acyclic 을 만족하는지 확인하는 방법으로, BFS나 DFS 외에도 union-find도 있다고 해요!
저는 그래프나 트리 문제를 풀 때마다 너무 당연하게 BFS/DFS로만 푸는 경향이 있었는데, 이번 문제를 통해 union-find로도 풀어보게 되었어서 함께 공유드려요~!
nodes[n].next = null; // 여기서 모든 노드의 next를 null로 변경 | ||
// 이렇게 안 하면 next 바꾸기 한 다음에 맨 마지막 노드의 next만 null로 바꿔야 함 | ||
n++ | ||
} |
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.
추가적인 해시맵을 생성해 연결 리스트의 각 노드를 O(1)으로 조회할 수 있게 함으로써 가독성 좋게 쓰인 풀이인 것 같습니다!
다만, 속도가 다른 투 포인터를 이용하여 연결 리스트를 절반으로 나누고 오른쪽 리스트를 뒤집은 후, 두 리스트를 첫 노드부터 번갈아가며 다시 엮어나가는 방식으로 푼다면 추가 공간을 사용하지 않고 공간 복잡도를 O(1)으로 최적화 할 수 있을 것 같아요!
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!