Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

2026年02月04日 문제 풀었어요#26

Merged
uyeon0 merged 1 commit intomain from
upload
Feb 4, 2026
Merged

2026年02月04日 문제 풀었어요 #26
uyeon0 merged 1 commit intomain from
upload

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Feb 4, 2026
edited by github-actions bot
Loading

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 49189번 문제 "가장 먼 노드" 해결

  • BFS 알고리즘을 사용한 최단 경로 탐색

  • 그래프 탐색 문제의 효율적인 해결 방법 구현


@uyeon0 uyeon0 added the Programmers Programmers 문제 풀이 label Feb 4, 2026
Copy link

github-actions bot commented Feb 4, 2026

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 등을 고려해볼 수 있습니다.

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;

Copy link

github-actions bot commented Feb 4, 2026

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion Impact
General
BFS 구현 간소화 및 코드 최적화

BFS 구현에서 불필요한 조건 제거하고 코드를 더 간결하게 만들 수 있습니다. neighbors.length === 0 체크는 불필요하며, 배열 순회
방식을 최적화할 수 있습니다.

Programmers/Level3/49189_가장_먼_노드.js [14-46]

 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);
 }
 
 const dist = Array.from({ length: n + 1 }, () => -1);
 
 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);
- });
+ for (const neighbor of adj[front]) {
+ if (dist[neighbor] === -1) {
+ dist[neighbor] = dist[front] + 1;
+ queue.push(neighbor);
+ }
+ }
 }
 
- const maxDist = Math.max(...dist);
- const answer = dist.filter((d) => d === maxDist).length;
- return answer;
+ const maxDist = Math.max(...dist.filter(d => d !== -1));
+ return dist.filter(d => d === maxDist).length;
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion removes the unnecessary neighbors.length === 0 check and uses a more concise for...of loop instead of forEach. It also slightly optimizes the maxDist calculation by filtering out -1 values.

Medium

@yoouyeon yoouyeon added ready-to-merge pr을 머지해주세요 labels Feb 4, 2026
@uyeon0 uyeon0 merged commit e99840e into main Feb 4, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

Programmers Programmers 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

Comments

AltStyle によって変換されたページ (->オリジナル) /