Algorithm/프로그래머스

[고득점kit/완전탐색] 피로도

녱녱 2023. 7. 16.

목차

[고득점kit/완전탐색] 피로도

class Solution {
    static boolean[] visited;
    static int max = 0;

    void explore(int piro, int[][] dungeons, int cnt) {
        // 모든 케이스 순회
        for (int i = 0; i < dungeons.length; i++) {
            int a = dungeons[i][0];
            int b = dungeons[i][1];

            // 기존에 방문 확인, 최소 피로도 확인
            if (visited[i] || piro < a) {
                continue;
            }

            // 방문
            visited[i] = true;
            explore(piro - b, dungeons, cnt + 1);
            // 다른 케이스를 위해 방문 취소
            visited[i] = false;
        }
        max = Math.max(max, cnt);
    }

    public int solution(int k, int[][] dungeons) {
        visited = new boolean[dungeons.length];

        explore(k, dungeons, 0);
        return max;
    }
    
}

진짜...재귀가 너무 싫구요 fatigue/exhaustion 이런게 바로 생각이 안나서 그냥 piro..

댓글