Muscardinus
전력망을 둘로 나누기 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/86971
function solution(n, wires) {
let answer = Infinity;
const link = {};
for (const wire of wires) {
const [from, to] = wire;
if (!link[from]) link[from] = [];
if (!link[to]) link[to] = [];
link[from].push(to);
link[to].push(from);
}
const track = (start, excep) => {
const visited = new Array(n + 1).fill(false);
const q = [start];
let cnt = 0;
visited[start] = true;
while(q.length) {
const a = q.pop();
cnt++;
for (let i = 0; i < link[a].length; i++) {
const target = link[a][i];
if (visited[target]) continue;
if (target === excep) continue;
visited[target] = true;
q.push(target);
}
}
return cnt;
}
for (const wire of wires) {
const [from, to] = wire;
const d = Math.abs(track(from, to) - track(to, from));
answer = answer > d ? d : answer;
}
return answer;
}
728x90
'알고리즘 문제 > [프로그래머스] Lv2' 카테고리의 다른 글
모음사전 (0) | 2022.04.07 |
---|---|
교점에 별 만들기 (0) | 2022.04.06 |
2개 이하 다른 비트 (0) | 2022.04.05 |
피로도 (0) | 2022.04.04 |
배달 (0) | 2022.04.04 |
Comments