Skip to content

[정봉찬] 8-9. 동전교환(복습풀이) #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

devwqc
Copy link
Contributor

@devwqc devwqc commented Sep 9, 2024

✔ 문제 제목: 동전교환

✔ 문제 유형: 재귀

✔ 문제 풀이 날짜: 2024-09-09

💡 문제 분석 요약

  • 거스름돈을 가장 적은 수의 동전으로 교환해주자.
  • 동전의 종류 개수 N(1<=N<=12), 거슬러 줄 금액 M(1<=M<=500)

💡 알고리즘 설계

  • 재귀 탐색하는 레벨을 동전을 뽑은 수로 생각한다.
  • 모든 동전이 모든 동전을 뽑을 수 있는 중복순열 방식을 생각한다.
  • 재귀에서 arr을 반복문 돌면서 sum에 현재 동전을 더하고 현재 레벨 + 1해서 다음 재귀를 탐색한다.
  • sum이 m을 넘어서거나 레벨이 answer보다 크거나 같으면 함수를 종료한다.
  • sum이 m에 도달했을 때 answer와 레벨 중에서 작은 값을 answer에 할당한다.

💡코드

function solution3(m, arr) {
  let answer = Number.MAX_SAFE_INTEGER;

  function DFS(L, sum) {
    if (sum > m || L >= answer) {
      return;
    }

    if (sum === m) {
      answer = Math.min(answer, L);
      return;
    }

    for (let i = 0; i < arr.length; i++) {
      DFS(L + 1, sum + arr[i]);
    }
  }

  DFS(0, 0);

  return answer;
}

console.log(solution3(15, arr)); // 3(5동전 3개)
console.log(solution3(19, arr)); // 5(5동전 3개, 2동전 2개)

💡 시간복잡도

  • ?

💡 느낀 점 or 기억할 정보

  • 오늘은 영등포 타임스퀘어가서 책 두 권 샀어요. 📚

Copy link
Contributor

@Seoin02 Seoin02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 코테 PR로 봉찬's 그날의 기록을 볼 수 있겠어요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants