-
Notifications
You must be signed in to change notification settings - Fork 0
Conversation
PR Reviewer Guide 🔍
🧪 No relevant tests
⚡ Recommended focus areas for review
성능 최적화
알고리즘 개선
성능 최적화
BFS 구현에서 queue 대신 배열과 head 포인터를 사용하여 메모리 효율성을 높였지만,
더 효율적인 큐 구현 또는 최적화 방법을 고려할 수 있습니다.
더 효율적인 큐 구현 또는 최적화 방법을 고려할 수 있습니다.
const queue = [1]; let head = 0; while (head < queue.length) { const front = queue[head++]; const neighbors = adj[front]; if (neighbors.length === 0) continue; neighbors.forEach((neighbor) => { if (dist[neighbor] !== -1) return; // 처음 방문하는 시점이 바로 최단거리이므로 주저없이 업데이트 // 방문 기록은 겸사겸사 dist[neighbor] = dist[front] + 1; queue.push(neighbor); }); }
알고리즘 개선
현재 구현은 O(V+E) 시간 복잡도를 가지며,
인접 리스트를 사용해 공간 복잡도도 최적화되어 있습니다.
추가로 early stopping 등을 고려해볼 수 있습니다.
인접 리스트를 사용해 공간 복잡도도 최적화되어 있습니다.
추가로 early stopping 등을 고려해볼 수 있습니다.
function solution(n, edge) { const adj = Array.from({ length: n + 1 }, () => []); for (const [a, b] of edge) { adj[a].push(b); adj[b].push(a); } // -1은 아직 모르는 거리를 의미함 // NOTE : BFS에서는 첫 방문 거리가 곧 최단거리이므로 (다익스트라처럼 갱신 x) Infinity 대신 -1을 쓴다. const dist = Array.from({ length: n + 1 }, () => -1); // BFS 시작 dist[1] = 0; // 시작 지점 const queue = [1]; let head = 0; while (head < queue.length) { const front = queue[head++]; const neighbors = adj[front]; if (neighbors.length === 0) continue; neighbors.forEach((neighbor) => { if (dist[neighbor] !== -1) return; // 처음 방문하는 시점이 바로 최단거리이므로 주저없이 업데이트 // 방문 기록은 겸사겸사 dist[neighbor] = dist[front] + 1; queue.push(neighbor); }); } const maxDist = Math.max(...dist); const answer = dist.filter((d) => d === maxDist).length; return answer;
PR Code Suggestions ✨Explore these optional code suggestions:
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Uh oh!
There was an error while loading. Please reload this page.
User description
오늘도 멋져요 👍✨
PR Type
Enhancement
Description
프로그래머스 49189번 문제 "가장 먼 노드" 해결
BFS 알고리즘을 사용한 최단 경로 탐색
그래프 탐색 문제의 효율적인 해결 방법 구현