
아래 프로그래머스 로고를 클릭하면 해당 문제로 이동합니다 😀
완전탐색으로 체크하면 될 것 같아서 파이썬에서 permutations를 이용해 풀었다.
js에서는 직접 구현해야하므로 ,,, 이전에 정리해뒀던 js에서 순열 구현를 참고했다.
풀이 알고리즘 과정은 다음과 같다.
1. 모든 던전 순열을 생성
2. 각 순열마다 탐험 가능한 던전 수 계산
3. 최댓값을 계속 갱신하여 반환
from itertools import permutations
def solution(k, dungeons):
result = 0
for permu in permutations(dungeons, len(dungeons)):
cur_tired = k
cnt = 0
for min_need, cost in permu:
if cur_tired >= min_need:
cur_tired -= cost
cnt += 1
result = max(result, cnt)
return result
const isValid = (s) => {
let stack = [];
const bracketMap = {')': '(', ']': '[', '}': '{'};
for(const ch of s){
if('([{'.includes(chfunction getPermutations(arr, selectNum) {
const result = [];
if (selectNum === 1) return arr.map((v) => [v]);
arr.forEach((fixed, idx, origin) => {
const rest = origin.slice(0, idx).concat(origin.slice(idx + 1));
const perms = getPermutations(rest, selectNum - 1);
const attached = perms.map((perm) => [fixed, ...perm]);
result.push(...attached);
});
return result;
}
function solution(k, dungeons) {
const permutations = getPermutations(dungeons, dungeons.length);
let maxCount = 0;
for (const perm of permutations) {
let curTired = k;
let count = 0;
for (const [minNeed, cost] of perm) {
if (curTired >= minNeed) {
curTired -= cost;
count += 1;
} else {
break;
}
}
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
