Algo Rhythm๐Ÿ•บ๐Ÿ’ƒ/Programmers

[Algo Rhythm๐Ÿ•บ๐Ÿ’ƒ] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ณ ๋“์  kit ๋‹จ์–ด๋ณ€ํ™˜

ALiNew 2020. 7. 23. 14:28

๐Ÿ“š ๋ฌธ์ œ ์„ค๋ช…

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ begin์—์„œ target์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๋ณ€ํ™˜ ๊ณผ์ •์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

1. ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ๋งŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
2. words์— ์žˆ๋Š” ๋‹จ์–ด๋กœ๋งŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด begin์ด hit, target๊ฐ€ cog, words๊ฐ€ [hot,dot,dog,lot,log,cog]๋ผ๋ฉด hit -> hot -> dot -> dog -> cog์™€ ๊ฐ™์ด 4๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ ๋ช‡ ๋‹จ๊ณ„์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ begin์„ target์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • ๊ฐ ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๊ฐ ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 3 ์ด์ƒ 10 ์ดํ•˜์ด๋ฉฐ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • words์—๋Š” 3๊ฐœ ์ด์ƒ 50๊ฐœ ์ดํ•˜์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ ์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด๋Š” ์—†์Šต๋‹ˆ๋‹ค.

  • begin๊ณผ target์€ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” 0๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

 

๐ŸŽฏ ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋‚˜

import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int solution(String begin, String target, String[] words) {
        if(!Arrays.stream(words).anyMatch(e -> e.equals(target))) return 0;
        
        int answer = 0;
        int root = 0,tIdx = 0;
        ArrayList<String> wordList = new ArrayList<>(Arrays.asList(words));
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        Queue<Integer> q = new LinkedList<>();
        
        if(!Arrays.stream(words).anyMatch(e -> e.equals(begin))) wordList.add(begin);
        
        boolean[] visited = new boolean[wordList.size()];
        
        // init graph
        for(int i = 0; i < wordList.size(); i++) {
            if(wordList.get(i).equals(begin)) root = i;
            if(wordList.get(i).equals(target)) tIdx = i;
            
            ArrayList<Integer> lst = new ArrayList<>();
            for(int j = 0; j < wordList.size(); j++) {
                if(i == j) continue;
                if(isOneDiff(wordList.get(i), wordList.get(j))) lst.add(j);
            }
           graph.add(lst);
        }
        
        // BFS
        q.add(root);
        q.add(null);
        while(!q.isEmpty()) {
            if(q.element() == null) {
                answer++;
                q.remove();
            }
            
            int parent = q.remove();
            visited[parent] = true;

            boolean found = false;
            
            for(int child : graph.get(parent)) {
                if(visited[child] == false) {
                    q.add(child);
                }
                if(child == tIdx) {
                    found = true;
                    break;
                }
            }
            
            if(found) break;
            
            q.add(null);
        }
        
        return answer + 1;
    }
    
    public boolean isOneDiff(String s1, String s2) {
        int nDiff = 0;
        for(int i = 0; i < s1.length(); i++) {
            if(s1.charAt(i) != s2.charAt(i)) nDiff++;
            if(nDiff > 1) return false;
        }
        return true;
    }
}

 

๐Ÿง ์™œ ์ €๋ ‡๊ฒŒ ํ’€์—ˆ๋‚˜

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ„๋‹จํ•˜๋‹ค.

๋จผ์ € ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง€๋Š” 'begin'๊ณผ ๋ฐฐ์—ด 'words'๋ฅผ ํ•ฉ์ณ์„œ ๋…ธ๋“œ๋“ค์˜ ์ง‘ํ•ฉ์„ ๋งŒ๋“ ๋‹ค.

๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ ์ด ๋…ธ๋“œ๋“ค์„ ์ด์šฉํ•ด์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š”๋ฐ ๊ทธ๋ž˜ํ”„๋Š” ArrayList๋ฅผ ์‚ฌ์šฉํ•œ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค. ์ •ํ™•ํžˆ ํ•œ ๊ธ€์ž๋งŒ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ ๊ธ€์ž์™€ ๋‹ค๋ฅธ ๊ธ€์ž๊ฐ€ ์ •ํ™•ํžˆ ํ•œ ๊ธ€์ž๋งŒ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์ด์–ด์ง€๋„๋ก ํ–ˆ๋‹ค.

๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“  ํ›„๋ถ€ํ„ฐ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. BFS๋ฅผ ํ†ตํ•ด target์„ ๊ฐ€์žฅ ๋จผ์ € ์ฐพ์•˜์„ ๋–„์˜ level์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. ์ด ๋•Œ ํ•œ ๊ฐ€์ง€ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์Šคํ‚ฌ์ด ํ์— null์„ ๋„ฃ์–ด์„œ BFS๋ฅผ ํ•˜๋ฉด์„œ level์„ ๊ตฌ๋ถ„ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

๐Ÿ“– ๋ฌด์—‡์„ ๋ฐฐ์› ๋‚˜

BFS๋ฅผ ํ•  ๋•Œ null์„ ๋„ฃ์–ด์„œ level์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์Šคํ‚ฌ์„ ๋ฐฐ์› ๋‹ค!