문제
풀이 1
function solution(k, dungeons) {
var answer = 0;
let combi = getCombination(dungeons, dungeons.length)
combi.map(v => {
let cnt = 0
let fatigue = k
v.map(target => {
if (fatigue >= target[0] && fatigue >= target[1]) {
fatigue -= target[1]
cnt++
}
})
answer = Math.max(answer, cnt)
})
return answer;
}
function getCombination(arr, seletNum) {
const results = []
if (seletNum===1) return arr.map(v=>[v])
arr.forEach( (fixed, index) => {
const rest = [...arr.slice(0,index), ...arr.slice(index+1) ]
const combinations = getCombination(rest, seletNum-1)
const attatch = combinations.map(combination => [fixed, ...combination])
results.push(...attatch)
})
return results
}
풀이 2
function solution(k, d) {
const N = d.length
const visited = new Array(N).fill(0)
let ans = 0
function dfs(k, cnt){
ans = Math.max(cnt, ans)
for (let j = 0; j < N; j++){
if (k >= d[j][0] && !visited[j]){
visited[j] = 1
dfs(k - d[j][1], cnt + 1)
visited[j] = 0
}
}
}
dfs(k, 0)
return ans;
}
풀이 3
function solution(k, dungeons) {
if (dungeons.length <= 0) return 0;
let maxDungeons = 0;
for (let i = 0; i < dungeons.length; i++) {
if (k >= dungeons[i][0]) {
let n = solution(
k - dungeons[i][1],
dungeons.slice(0, i).concat(dungeons.slice(i + 1))
);
if (n + 1 > maxDungeons) {
maxDungeons = n + 1;
}
if (maxDungeons >= dungeons.length) return maxDungeons;
}
}
return maxDungeons;
}