피로도

LJM·2023년 8월 18일
0

programmers

목록 보기
67/92

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

완탐
백트랙킹으로 풀었다..

class Solution {
    
    private int answer = 0;
    
    public int solution(int k, int[][] dungeons) {
        
        boolean[] visit = new boolean[dungeons.length];
        
        brute(0, k, dungeons, visit);

        return answer;
    }
    
    public void brute(int depth, int k, int[][] dungeons, boolean[] visit)
    {
        answer = Math.max(answer, depth);
        
        if(depth == visit.length)
        {          
            return;
        }
        
        for(int i = 0; i < visit.length; ++i)
        {           
            if(visit[i])
                continue;
            
            int need = dungeons[i][0];
            if(k < need)
                continue;
            
            int cost = dungeons[i][1];
            visit[i] = true;    
            brute(depth+1, k-cost, dungeons, visit);
            visit[i] = false;
        }
        
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글