https://programmers.co.kr/learn/courses/30/lessons/49189
let graph = {}, dist;
const dfs = (target, dp = 1) => {
graph[target].filter(e => !dist[e] || dist[e] > dp).forEach(e => {
dist[e] = dp;
dfs(e, dp + 1)
})
}
function solution(n, edge) {
edge.forEach(([a, b]) => {
graph[a] = graph[a] ? [...graph[a], b] : [b];
graph[b] = graph[b] ? [...graph[b], a] : [a];
})
dist = {1: -1};
dfs(1);
const distArr = Object.values(dist);
const max = distArr.reduce((ac, v) => ac > v ? ac : v, 0)
return distArr.filter(e => e === max).length;
}
하지만 런타임 에러 발생... 너무 깊어져서 그런가..
bfs를 통해서 탐색
방문 배열을 따로 만들지 않고 거리배열로 탐색하기에
거리가 0인 노드, e !== 1
이 부분을 주의해야 한다.
function solution(n, edge) {
const graph = {}, dist = {1: 0};
edge.forEach(([a, b]) => {
graph[a] = graph[a] ? [...graph[a], b] : [b];
graph[b] = graph[b] ? [...graph[b], a] : [a];
})
const q = [1];
while (q.length) {
const cur = q.shift();
const nextDist = dist[cur] + 1;
graph[cur].filter(e => e !== 1 && (!dist[e] || dist[e] > nextDist)).forEach(e => {
dist[e] = nextDist;
q.push(e);
})
}
const distArr = Object.values(dist);
const max = distArr.reduce((ac, v) => ac > v ? ac : v, 0)
return distArr.filter(e => e === max).length;
}
function solution(n, edge) {
const graph = {}, dist = { 1: 0 };
edge.forEach(([a, b]) => {
graph[a] = graph[a] ? [...graph[a], b] : [b];
graph[b] = graph[b] ? [...graph[b], a] : [a];
})
const q = [1];
let nextDist;
while (q.length) {
const cur = q.shift();
nextDist = dist[cur] + 1;
graph[cur].filter(e => e !== 1 && !dist[e]).forEach(e => {
dist[e] = nextDist;
q.push(e);
})
}
return Object.values(dist).filter(e => e === nextDist-1).length;
}
테스트 캐이스는 통과 되더라 ㅋㅋㅋ
function solution(n, edge) {
const visit = {1: 1};
const dp = {1: 0};
const q = [1];
let deep;
while (q.length) {
const cur = q.shift();
deep = dp[cur];
const nexts = [].concat(...edge.filter(e => e.find(node => node === cur)));
nexts.filter(nt => !visit[nt]).forEach(nt => {
visit[nt] = 1;
dp[nt] = deep + 1;
q.push(nt);
});
}
return Object.values(dp).filter(e => e === deep).length;
}