관리 메뉴

Lennon FE

[ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] ν”Όλ‘œλ„ - μžλ°”μŠ€ν¬λ¦½νŠΈ(js) λ³Έλ¬Έ

πŸ”₯ Algorithm/Programmers

[ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] ν”Όλ‘œλ„ - μžλ°”μŠ€ν¬λ¦½νŠΈ(js)

Lennon 2022. 3. 14. 17:40
728x90
λ°˜μ‘ν˜•

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

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - ν”Όλ‘œλ„

XXκ²Œμž„μ—λŠ” ν”Όλ‘œλ„ μ‹œμŠ€ν…œ(0 μ΄μƒμ˜ μ •μˆ˜λ‘œ ν‘œν˜„ν•©λ‹ˆλ‹€)이 있으며, 일정 ν”Όλ‘œλ„λ₯Ό μ‚¬μš©ν•΄μ„œ λ˜μ „μ„ νƒν—˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ•Œ, 각 λ˜μ „λ§ˆλ‹€ νƒν—˜μ„ μ‹œμž‘ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„"와 던

programmers.co.kr

 

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
λ°˜μ‘ν˜•
Comments