1. ๋ฌธ์ œ : https://school.programmers.co.kr/learn/courses/30/lessons/172927

 

2. ํ’€์ด

5 ์ง€๋‚ ๋•Œ๋งˆ๋‹ค dfs์— ๋„ฃ๋Š” tool๊ฐ’ ๋ฐ”๊ฟ”์ค€๋Š”๊ฑฐ ์•„๋ฆ„๋‹ค์šธ ์ •๋„๋‹ค...

 

3. ์ฝ”๋“œ

class Solution {
    static int min = Integer.MAX_VALUE;
    public int solution(int[] picks, String[] minerals) {
        int answer = 0;
        int dia = picks[0];
        int iron = picks[1];
        int stone = picks[2];
      
        if (dia > 0)
            dfs(0, "d", 0, minerals, dia-1, iron, stone);
        if (iron > 0)
            dfs(0, "i", 0, minerals, dia, iron-1, stone);
        if (stone > 0)
            dfs(0, "s", 0, minerals, dia, iron, stone-1);
        
        return min;
    }
    
    public void dfs(int fatigue, String tool, int index, String[] minerals, int dia, int iron, int stone) {
        if ( index >= minerals.length ) {
            min = Math.min(min, fatigue);
            return;
        }
        
        if (minerals[index].equals("diamond")) {
            if ("d".equals(tool)) {
                fatigue += 1;
            } else if ("i".equals(tool)) {
                fatigue += 5;
            } else {
                fatigue += 25;
            }
        } else if (minerals[index].equals("iron")) {
            if ("d".equals(tool)) {
                fatigue += 1;
            } else if ("i".equals(tool)) {
                fatigue += 1;
            } else {
                fatigue += 5;
            }
        } else if (minerals[index].equals("stone")) {
            if ("d".equals(tool)) {
                fatigue += 1;
            } else if ("i".equals(tool)) {
                fatigue += 1;
            } else {
                fatigue += 1;
            }
        }
        
        if ((index + 1) % 5 == 0) {
            if ((dia == 0 && iron == 0 && stone == 0) ) {
                min = Math.min(min, fatigue);
                return;
            }
            
            if (dia > 0) {
                dfs (fatigue, "d", index+1, minerals, dia-1, iron, stone);
            }
            
            if (iron > 0) {
                dfs(fatigue, "i", index+1, minerals, dia, iron-1, stone);
            }
            
            if (stone > 0) {
                dfs(fatigue, "s", index+1, minerals, dia, iron, stone-1);
            }
        } else {
            dfs(fatigue, tool, index+1, minerals, dia, iron, stone);
        }
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ