😎풀이

  1. 간선 생성
  2. 간선 연결
  3. 도착지를 사전 기준 오름차 순 정렬
  4. 깊이 우선탐색
    4-1. 도착지 중 사전 순 빠른 공항을 우선 적으로 탐색
    4-2. 탐색 종료 후 result에 추가
  5. 추가된 result를 역순으로 반환(JKF 공항부터 시작되도록)
function findItinerary(tickets: string[][]): string[] {
    const graph = new Map<string, string[]>()
    for(const [from, to] of tickets) {
        if(!graph.has(from)) graph.set(from, [])
        graph.get(from).push(to)
    }
    for(const [key, value] of graph) value.sort()
    const result = []
    function dfs(target: string) {
        const airport = graph.get(target)
        while(airport && airport.length) dfs(airport.shift())
        result.push(target) 
    }
    dfs("JFK")
    return result.reverse()
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글