본문 바로가기

Programmers

[프로그래머스][JAVA]Lv. 2 - 피로도

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

가지고 있는 피로도 k와 dungeons{최소 필요 피로도, 소모 피로도}를 가지고 최대한으로 탐험할 수 있는 던전을 찾기

완전탐색으로 찾기 위해 dfs를 이용하여 문제를 해결하였다.

class Solution {
    static int max = 0;
    public int solution(int k, int[][] dungeons) {
        boolean[] visit = new boolean[dungeons.length];
        dfs(k, dungeons, visit, 0);
        return max;
    }
    
    public void dfs(int k, int[][] dungeons, boolean[] visit, int cnt) {
        for (int i = 0; i < dungeons.length; i++) {
            if (k >= dungeons[i][0] && !visit[i]) {
                visit[i] = true;
                dfs(k - dungeons[i][1], dungeons, visit, cnt + 1);
                visit[i] = false;
            }
            max = Math.max(max, cnt);
        }
    }
}