Skip to content

2026-02-04 문제 풀었어요 - #26

Merged
uyeon0 merged 1 commit into
mainfrom
upload
Feb 4, 2026
Merged

2026-02-04 문제 풀었어요#26
uyeon0 merged 1 commit into
mainfrom
upload

Conversation

@uyeon0

@uyeon0uyeon0 commented Feb 4, 2026

Copy link
Copy Markdown
Collaborator

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

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

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

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


@uyeon0uyeon0 added the Programmers Programmers 문제 풀이 label Feb 4, 2026
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

🧪 No relevant tests
⚡ Recommended focus areas for review

성능 최적화

BFS 구현에서 queue 대신 배열과 head 포인터를 사용하여 메모리 효율성을 높였지만,
더 효율적인 큐 구현 또는 최적화 방법을 고려할 수 있습니다.

constqueue=[1];lethead=0;while(head<queue.length){constfront=queue[head++];constneighbors=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 등을 고려해볼 수 있습니다.

functionsolution(n,edge){constadj=Array.from({length: n+1},()=>[]);for(const[a,b]ofedge){adj[a].push(b);adj[b].push(a);}// -1은 아직 모르는 거리를 의미함// NOTE : BFS에서는 첫 방문 거리가 곧 최단거리이므로 (다익스트라처럼 갱신 x) Infinity 대신 -1을 쓴다.constdist=Array.from({length: n+1},()=>-1);// BFS 시작dist[1]=0;// 시작 지점constqueue=[1];lethead=0;while(head<queue.length){constfront=queue[head++];constneighbors=adj[front];if(neighbors.length===0)continue;neighbors.forEach((neighbor)=>{if(dist[neighbor]!==-1)return;// 처음 방문하는 시점이 바로 최단거리이므로 주저없이 업데이트// 방문 기록은 겸사겸사dist[neighbor]=dist[front]+1;queue.push(neighbor);});}constmaxDist=Math.max(...dist);constanswer=dist.filter((d)=>d===maxDist).length;returnanswer;

@github-actions

Copy link
Copy Markdown

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

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

Labels

ProgrammersProgrammers 문제 풀이ready-to-mergepr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@uyeon0@yoouyeon