π₯ Algorithm/Programmers
[νλ‘κ·Έλλ¨Έμ€] νΌλ‘λ - μλ°μ€ν¬λ¦½νΈ(js)
Lennon
2022. 3. 14. 17:40
728x90
λ°μν
https://programmers.co.kr/learn/courses/30/lessons/87946?language=javascript
function solution(k, dungeons) {
const result = getPermutations(dungeons, dungeons.length);
let maxDungeon = 0;
result.forEach((v) => {
let fatigue = k;
let count = 0;
for (let i = 0; i < v.length; i++) {
if (fatigue >= v[i][0]) {
count++;
fatigue -= v[i][1];
} else {
break;
}
}
maxDungeon = Math.max(maxDungeon, count);
});
return maxDungeon;
}
const getPermutations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((el) => [el]);
arr.forEach((fixed, index, origin) => {
const rest = [...origin.slice(0, index), ...origin.slice(index + 1)];
const permutations = getPermutations(rest, selectNumber - 1);
const attached = permutations.map((el) => [fixed, ...el]);
results.push(...attached);
});
return results;
};
DPλ¬Έμ μΈκ° μΆμμ§λ§ DPλ‘ ν΄κ²°μ΄ μλ κ² κ°μκ³ , DFSλ‘ νμ΄λ λμ§λ§,
μ΅λ λμ μκ° 8μΈ κ±Έ 보μ μμ νμμΌλ‘ νμ΄λ μκ΄μμ κ² κ°μ μμ νμμΌλ‘ νμλ€.
λͺ¨λ μμλ₯Ό ꡬν ν μμ°¨μ μΌλ‘ λ리면 λ!
728x90
λ°μν