Notice
Recent Posts
Recent Comments
Link
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ์ฑŒ๋ฆฐ์ง€ 12์ฃผ์ฐจ (js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ์ฑŒ๋ฆฐ์ง€ 12์ฃผ์ฐจ (js)

Lennon 2021. 10. 26. 22:02
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/87946?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 12์ฃผ์ฐจ

XX๊ฒŒ์ž„์—๋Š” ํ”ผ๋กœ๋„ ์‹œ์Šคํ…œ(0 ์ด์ƒ์˜ ์ •์ˆ˜๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค)์ด ์žˆ์œผ๋ฉฐ, ์ผ์ • ํ”ผ๋กœ๋„๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋˜์ „์„ ํƒํ—˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ๊ฐ ๋˜์ „๋งˆ๋‹ค ํƒํ—˜์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ "์ตœ์†Œ ํ•„์š” ํ”ผ๋กœ๋„"์™€ ๋˜

programmers.co.kr

 

function solution(k, dungeons) {
    let answer = 0;
    let permutationDungeons = permutation(dungeons, dungeons.length);
    
    permutationDungeons = permutationDungeons.map((v) => v.split(" ").map((vi) => vi.split(",")));
    
    for(let i = 0; i < permutationDungeons.length; i++){
        let fatigue = k;
        let count = 0;
        for(let j = 0; j < permutationDungeons[i].length; j++){
              if(fatigue >= permutationDungeons[i][j][0]){
                  fatigue -= permutationDungeons[i][j][1];
                  count++;
              }
              if(fatigue < 0){
                  break;
              }
        }
        answer = Math.max(count, answer);
    }
    return answer;
}

function permutation(arr, selectNum){
        let result = [];
        if(selectNum === 1) return arr.filter((v)=> [v]);

        arr.forEach((v,idx,arr)=>{
            const fixer = v;
            const restArr = arr.filter((val, index)=> index !== idx);
            const permuArr = permutation(restArr, selectNum-1);
            const combineFixer = permuArr.map((v)=> fixer + " " + v);
            result.push(...combineFixer);
        })
        return result;
}

 

๋ฌธ์ œ์— ๋˜์ „์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 8์ด๋ผ ๋ช…์‹œ๋˜์–ด ์žˆ์–ด ์™„์ „ํƒ์ƒ‰์„ ์ด์šฉํ•ด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

dfs๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋ณด๋‹ค ํšจ์œจ์„ฑ์€ ๋–จ์–ด์กŒ๋‹ค ใ… ใ… ใ… 

 

๋‹ค์Œ์— dfs๊ฐ€ ์ต์ˆ™ํ•ด์ง€๋ฉด ์ด์šฉํ•ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•
Comments