프로그래머스 #10

DFS / BFS - 여행경로

function solution(tickets) {
    const list = {};
    const path = ["ICN"];
    
    tickets.sort();
    
    for (let [from, to] of tickets){
        if(!list[from]) list[from] = [to];
        else list[from].push(to);
    }
    
    // for (let from in list){
    //     if(list[from].length !== 1) list[from].sort();
    // }
    
    const queue = [];
    const enqueue = (el) => queue.push(el);
    const dequeue = () => queue.shift();
    
    enqueue(list["ICN"].shift());
    
    while(queue.length > 0){
        const from = dequeue();
        
        path.push(from);
        
        
        if(!list[from] || list[from].length === 0) break;
        
        enqueue(list[from].shift());
    }
    
    return path;
}

통과안된 케이스의 입력값을 모르니 정확히 어떻게 틀렸는지 아직 감이 오지 않는다

아마 정렬 순서가 잘못됬지 싶다

테스트 케이스가 더 필요할 것 같다

또는 DFS로 풀어봐야지

좋은 웹페이지 즐겨찾기