[Algorithm] programmers-132266-๋ถ€๋Œ€๋ณต๊ท€

Photo by Pat Kay on Unsplash

[Algorithm] programmers-132266-๋ถ€๋Œ€๋ณต๊ท€

ยท

2 min read

/*
1. ๊ฐ source์— ๋Œ€ํ•ด result๋ฅผ ๊ตฌํ•ด์•ผํ•œ๋‹ค
2. ๋ชจ๋“ ๊ฐ€์ค‘์น˜๊ฐ€ 1์ด๋‹ˆ ์šฐ์„ BFS๋กœ ํ’€์–ด๋ณด๊ณ  ์•ˆ๋˜๋ฉด ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ.
*/
class Solution {
    static ArrayList<ArrayList<Integer>> roadList = new ArrayList<>();
    static int N;
    static int target;
    static int[] dist;

    public int[] solution(int n, int[][] roads,int[] sources, int destination) {
        dist = new int[n+1];
        Arrays.fill(dist,-1);
        int[] answer = new int[sources.length];
        N = n;
        target = destination;
        //mapํฌ๊ธฐ๋งŒํผ arrayList๋งŒ๋“ค๊ธฐ
        for(int i=0;i<=n;i++){
            roadList.add(new ArrayList<>());
        }

        //๊ฐroad์ €์žฅ
        for(int i=0;i<roads.length;i++){
            int from = roads[i][0];
            int to = roads[i][1];
            roadList.get(from).add(to);
            roadList.get(to).add(from);
        }

        bfs(destination);

        for(int i=0;i<sources.length;i++)
            answer[i] = dist[sources[i]];


        return answer;
    }

    static void bfs(int start){
        boolean[] visited = new boolean[N+1];
        Queue<Integer> q = new LinkedList<>();
        q.offer(start);
        int level = 0;
        visited[start] = true; //๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

        while(!q.isEmpty()){
            int size = q.size();
            for(int reps=0 ; reps<size ; reps++){
                int source = q.poll();
                dist[source] = level;

                ArrayList<Integer> list = roadList.get(source); //ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๊ธธ ํƒ์ƒ‰
                for(int i=0;i<list.size();i++){
                    int next = list.get(i);
                    if(!visited[next]){
                        visited[next] = true; //๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
                        q.offer(next);
                    }
                }
            }
            level++;
        }
    }
}
  • ๊ฐ€์ค‘์น˜๊ฐ€ 1์ผ๋•Œ์—๋Š” BFS๋กœ๋„ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•ด๋ณด๊ณ  ํ˜น์‹œ ์•ˆ๋˜๋ฉด ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ํ’€์–ด๋ณด๋ ค๊ณ ํ–ˆ๋‹ค.

  • ํ•ต์‹ฌ์€ ๊ฐ source์˜ ์›์†Œ๋งˆ๋‹ค bfs๋ฅผ ๋งค๋ฒˆ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ destination์—์„œ ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ํ•œ๋ฒˆ๋งŒ ๊ตฌํ•˜๊ณ  ์ด๋ฅผ dist๋ฐฐ์—ด์— ๊ธฐ๋กํ•œ๋’ค ํ’€์–ด์•ผ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚œ๋‹ค. source[i] -> destination์„ ๊ฐ source๋งˆ๋‹ค ๊ตฌํ•˜์ง€ ๋ง๊ณ  destination -> source[i]๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•œ ๋’ค ๋‹ต ์ถœ๋ ฅ!

  • BFS๋ฅผ ํ• ๋•Œ level์„ ์„ ์–ธํ•ด์„œ ๊นŠ์ด๊ฐ€ 1์”ฉ ๊นŠ์–ด์งˆ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€์‹œ์ผœ์คฌ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ช‡๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณค๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ๋˜ํ•œ ํ์—์„œ ๊บผ๋‚ผ ๋•Œ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ–ˆ๋Š”๋ฐ ๊ทธ๋Ÿฌ๋ฉด ํ์— ์ค‘๋ณต๋œ ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ  ์ž˜๋ชป๋œ ๊ฑฐ๋ฆฌ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ offer()ํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ณ„๊ฑฐ ์•„๋‹ˆ๊ธด ํ•œ๋ฐ size๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ํ• ๋‹นํ•ด์„œ for(int reps=0 ; reps<size ; reps++) ๋ผ๋Š” ๊ตฌ๋ฌธ์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  for(int reps=0; reps<q.size() ; reps++) ๋กœ ํ•ด๋ฒ„๋ฆฌ๋ฉด ํ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ˆ˜์‹œ๋กœ ๋ณ€ํ•ด์„œ level์„ ์ •ํ™•ํžˆ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค.

  • ์ถ”๊ฐ€๋กœ 2์ฐจ์› ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ 2์ฐจ์› ArrayList๋ฅผ ์จ์„œ ์กด์žฌํ•˜๋Š” ๊ธธ๋งŒ ํ‘œ์‹œํ•˜๋„๋กํ–ˆ๋‹ค. ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 100,000๊ฐœ์ด๋‹ˆ๊นŒ 100,000 * 100,000 ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค.

ย