๐Ÿ”Žํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ

๋ฐ•๋ฏผ์šฐยท2023๋…„ 6์›” 10์ผ
0

๋ฌธ์ œ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ

์นดํ…Œ๊ณ ๋ฆฌ : ๊ทธ๋ž˜ํ”„

์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์—ฐ์Šต, https://school.programmers.co.kr/learn/challenges


โœ๏ธ ๋‚ด ํ’€์ด

[[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ทธ๋ž˜ํ”„์˜ ๊ฐ ๋…ธ๋“œ์™€ ๋…ธ๋“œ ๊ฐ„์˜ ๊ฐ„์„  ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด๋ฅผ ํ†ตํ•ด ๋…ธ๋“œ 1๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ๋–จ์–ด์ง„ ๋…ธ๋“œ๋“ค์„ ์ฐพ์•„์•ผํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

"๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„" ๋…ธ๋“œ๋“ค์„ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋…ธ๋“œ 1๊ณผ ๋‚˜๋จธ์ง€ ๋…ธ๋“œ๋“ค๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ค ๊ตฌํ•ด์„œ ๋น„๊ตํ•ด์•ผ ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

๊ฐ ๋…ธ๋“œ๋“ค๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด, ๋…ธ๋“œ๋“ค์ด ์„œ๋กœ ์–ด๋””๊นŒ์ง€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š”์ง€ ๋‹ค ํƒ์ƒ‰ํ•ด๋ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ, BFS ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.


  1. BFS๋ฅผ ์œ„ํ•œ Queue ์ž๋ฃŒ๊ตฌ์กฐ ์ง์ ‘ ๊ตฌํ˜„

  2. ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„

const graph = Array.from( // ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„
        Array(n+1), // 1~n ๊นŒ์ง€ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ
        () => []
);

for(let [i,j] of edge){ // ๊ทธ๋ž˜ํ”„ ์ดˆ๊ธฐํ™” 
    graph[i].push(j);
    graph[j].push(i);
}

  1. BFS๋กœ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
let dist = new Array(n+1).fill(-1); // 1๋ฒˆ ๋…ธ๋“œ์™€ 1 ~ n๋ฒˆ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ  
let queue = new Queue();

dist[1] = 0; 
let max_dist = 0; // 1๋ฒˆ ๋…ธ๋“œ์™€ 1 ~ n๋ฒˆ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ์ค‘ ์ตœ๋Œ“๊ฐ’

queue.enqueue([1, 0]); // 1๋ฒˆ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ํƒ์ƒ‰ ์‹œ์ž‘ 

while(queue.size()){
    let [node, now_dist] = queue.dequeue(); // [1, 0]
    
    for(let x of graph[node]){ // 2, 3
        if(dist[x] !== -1) continue;// ๋ฐฉ๋ฌธ ์ „ && ๊ฑฐ๋ฆฌ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋  ์ˆ˜ ์žˆ์„ ๋•Œ 
        
        dist[x] = now_dist+1; // ๋ฐฉ๋ฌธ ํ‘œ์‹œ

        if(max_dist < now_dist+1){ 
            max_dist = now_dist+1;
        }

        queue.enqueue([x, now_dist+1]);
    }
}
  • ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฉด, ๊ทธ ๋‹ค์Œ ๋ฐฉ๋ฌธ์—๋Š” ํƒ์ƒ‰ํ•˜๋ฉด์„œ 1๋ฒˆ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ์ฆ๊ฐ€ํ–ˆ์„ ๊ฒƒ์ด๋‹ˆ, ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ผ ์—…๋ฐ์ดํŠธ ๋  ์ƒํ™ฉ์€ ์—†์„ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์ฒ˜์Œ ๋ฐฉ๋ฌธ์ธ ๋…ธ๋“œ๋งŒ Queue์— ๋„ฃ์–ด์ฃผ๋ฉด์„œ ํƒ์ƒ‰ํ•œ๋‹ค.
  • ๋‚˜์ค‘์— 1๋ฒˆ ๋…ธ๋“œ์™€ ์ œ์ผ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š” ๋…ธ๋“œ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ, 1๋ฒˆ ๋…ธ๋“œ์™€ 1 ~ n๋ฒˆ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜ max_dist๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ณ , ๊ฐ node๋“ค์„ ๋ฐฉ๋ฌธํ•  ๋•Œ๋งˆ๋‹ค ์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅ ์‹œ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

  1. ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋…ธ๋“œ๋“ค์˜ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ
let answer = 0;
dist.forEach((dist) => {
    if(dist === max_dist)
        answer++;
})

return answer;

โœ’๏ธ Solution

๋ฐฐ์—ด๋กœ ํ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ shift ์—ฐ์‚ฐ์€ O(n)์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” queue์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๋งŒ์•ฝ ์š”์†Œ๊ฐ€ ์ ์„ ๊ฒฝ์šฐ์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์—์„œ ์–ด๋Š์ •๋„ ์ตœ์ ํ™”๋ฅผ ํ•ด์ค€๋‹ค.

=> ๋”ฐ๋ผ์„œ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ๋ฐฐ์—ด์˜ shift๋ฅผ ์ด์šฉํ•ด์„œ queue๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ๋‹ค !


Solution์„ ๋ณด๊ณ , ๋‚ด ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•ด๋ณด์•˜๋‹ค.

for(let [i,j] of edge)  =>  for(const[src, dest] of edge)

๋‚˜๋Š” BFS๋ฅผ ๋Œ๋ฉด์„œ queue์— [node ๋ฒˆํ˜ธ, ๊ฑฐ๋ฆฌ] ๋ฅผ ๋„˜๊ฒจ์คฌ๋Š”๋ฐ, ๊ฑฐ๋ฆฌ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ๊ฐ ๋…ธ๋“œ๋ฅผ queue์— ๋„ฃ์„ ๋•Œ ๋”ฐ๋กœ distance๋ฅผ ๊ณ„์‚ฐํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

distance[ํ˜„์žฌ ๋…ธ๋“œ] = distane[์ด์ „ ๋…ธ๋“œ] + 1

๋‚˜๋Š” max_dist ๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ๋’€์ง€๋งŒ ํ•˜์œ„ ๋ฐฉ์‹์œผ๋กœ ๋” ์งง๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค !

const max = Math.max(...distance);
return distance.filter(item => item === max).length;
profile
๊พธ์ค€ํžˆ, ๊นŠ๊ฒŒ

0๊ฐœ์˜ ๋Œ“๊ธ€