[Algorithm - Programmers] 여행경로

nunu·2023년 12월 17일
0

Algorithm

목록 보기
119/142

https://school.programmers.co.kr/learn/courses/30/lessons/43164

제출 코드

import java.util.ArrayList;
import java.util.Collections;
class Solution {
    ArrayList<String> list = new ArrayList<>();
    boolean[] used;
    public String[] solution(String[][] tickets) {
        used = new boolean[tickets.length];
        dfs(0, "ICN", "ICN", tickets);

        Collections.sort(list);

        return list.get(0).split(" ");
    }
    void dfs(int depth, String now, String path, String[][] tickets) {
        if (depth == tickets.length) {
            list.add(path);
            return;
        }
        for (int i = 0; i < tickets.length; i++) {
            if (!used[i] && now.equals(tickets[i][0])) {
                used[i] = true;
                dfs(depth + 1, tickets[i][1], path + " " + tickets[i][1], tickets);
                used[i] = false;
            }
        }
    }
}
profile
Hello, I'm nunu

0개의 댓글