์ตœ๋Œ€ 1 ๋ถ„ ์†Œ์š”

๐Ÿ“ [Lv2_87946] ํ”ผ๋กœ๋„

class Solution {
    public boolean[] check;
    public int answer = 0;
    
    public int solution(int k, int[][] dungeons) {
        
        check = new boolean[dungeons.length];
        dfs(0,k,dungeons);
        return answer;
    }
    
    public void dfs(int depth, int k, int[][] dungeons){
        for(int i=0; i<dungeons.length; i++){
            // ์ด๋ฏธ ์™”๋Š” ๊ณณ์ด๋ฉด ํ†ต๊ณผ
            if(check[i]) continue;
            // ์ตœ์†Œ ํ•„์š” ํ”ผ๋กœ๋„๊ฐ€ ์—†์œผ๋ฉด ํ†ต๊ณผ
            if(dungeons[i][0] > k) continue;
            
            // ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
            check[i] = true;
            dfs(depth+1,k-dungeons[i][1], dungeons);
            // ๋ฐฑํŠธ๋ž˜ํ‚น
            check[i] = false;
        }
        
        answer = Math.max(answer,depth);
    }
}

๐Ÿค” ๋‚˜์˜ ์ƒ๊ฐ

์™„์ „ ํƒ์ƒ‰์„ ํ†ตํ•ด ํ‘ผ ๋ฌธ์ œ์ด๋‹ค.
๋งŒ์•ฝ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ธธ์—ˆ๋‹ค๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์„ ํ…Œ์ง€๋งŒ ๋‹คํ–‰ํžˆ 8๊ฐœ๊ฐ€ ์ตœ๋Œ€ ๊ฐœ์ˆ˜์—ฌ์„œ ์™„์ „ ํƒ์ƒ‰์„ ํ•ด๋„ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค.
dfs๋ฅผ ์ด์šฉํ•ด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ๋ฐฐ์—ด์„ ๋Œ์•„์ฃผ๋ฉฐ ๋งˆ์ง€๋ง‰์— ๊ฐ€์žฅ ํฐ depth๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.