Skip to content

[std-freejia] week04 solutions #1822

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
Open

Conversation

std-freejia
Copy link
Contributor

@std-freejia std-freejia commented Aug 13, 2025

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

Comment on lines +19 to +20
// 길이가 둘다 null 이면, return null
if (list1 == null && list2 == null) return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

생략해도 상관없지만 명시해 주신 것 같네요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

안녕하세요 리뷰어님 코멘트 감사합니다!

Comment on lines +22 to +23
ListNode dummy = new ListNode(0); // 시작점
ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ListNode dummy = new ListNode(0); // 시작점
ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터
ListNode dummy = new ListNode(); // 시작점

둘다 똑같은 시작점이므로 // 시작점 뒤에 실제 작업용 포인터라는 설명은 틀린 것 같아요.
한 줄로 합쳐도 되겠는데 명시해 주신 것 같네요.
또한 0을 안 넘겨도 무방하겠군요.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dummy 는 고정된 시작점이고, output 은 이어붙일 포인터라서 따로 관리했습니다.
한 줄로 합치려고 했는데, accept 되지 못했습니다

Copy link
Contributor

Choose a reason for hiding this comment

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

오, 맞네요! 제가 잘못 생각했습니다.
output은 Iterator의 역할이군요.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

제 코드에서는 더 나은 네이밍이 필요한 것 같네요. 리뷰 감사합니다!

* 최대 depth 까지 가봐야 하므로 DFS 탐색 이용
*
* 모든 노드를 순회해야 하므로 시간복잡도 O(N)
* 재귀 호출 스택 프레임 (= 트리 최대 높이 height) 만큼 공간복잡도 O(H)
Copy link
Contributor

Choose a reason for hiding this comment

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

이 문제에서는 H = O(N)이라고 봐도 되겠네요.

Comment on lines +2 to +3
* BFS 는 너비 우선이므로, 가장 얕은 깊이로 목표 금액을 만족하면 '최소 동전 개수'를 사용한 것입니다.
* 너비 == 사용한 동전 개수
Copy link
Contributor

Choose a reason for hiding this comment

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

사용한 동전 개수 또는 최소 동전 개수는 너비 우선 탐색 트리의 너비가 아니라 깊이 또는 Level 아닐까요?
너비 즉 연결된 간선 수는 총 동전 개수 coins.length 같습니다.
그래서 시간 복잡도는 O(V+E) = O(amount + amount * coins.length) = O(amount * coins.length) 같네요.

Copy link
Contributor

@yhkee0404 yhkee0404 left a comment

Choose a reason for hiding this comment

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

반복문을 통한 순차 탐색 대신 0-1 BFS를 통해 평균적인 시간을 단축하신 것 같습니다.
DFS, BFS, 0-1 BFS 중 무엇이 더 유리할지는 잘 모르겠어요.
그런데 해시 자료구조를 사용하면 평균적인 공간도 줄일 수 있을 것 같군요.

Arrays.sort(coins); // 작은 동전부터 더해본다

// 이미 만들어본 '합' 은 지나가도록.
boolean[] visitedAmount = new boolean[amount + 1];
Copy link
Contributor

@yhkee0404 yhkee0404 Aug 16, 2025

Choose a reason for hiding this comment

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

#1822 (review)

Suggested change
boolean[] visitedAmount = new boolean[amount + 1];
Set<Integer> visitedAmount = new HashSet<>();

Queue<Integer> amountQueue = new ArrayDeque<>();

amountQueue.add(0); // 시작 금액 0
visitedAmount[0] = true;
Copy link
Contributor

@yhkee0404 yhkee0404 Aug 16, 2025

Choose a reason for hiding this comment

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

#1822 (review)

Suggested change
visitedAmount[0] = true;
visitedAmount.add(0);

Comment on lines +45 to +46
if (tempAmount < amount && !visitedAmount[tempAmount]) {
visitedAmount[tempAmount] = true;
Copy link
Contributor

@yhkee0404 yhkee0404 Aug 16, 2025

Choose a reason for hiding this comment

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

#1822 (review)

Suggested change
if (tempAmount < amount && !visitedAmount[tempAmount]) {
visitedAmount[tempAmount] = true;
if (tempAmount < amount && !visitedAmount.contains(tempAmount)) {
visitedAmount.add(tempAmount);

public int coinChange(int[] coins, int amount) {
if (amount == 0) return 0; // 목표 금액 0인 경우, '0개' 리턴

Arrays.sort(coins); // 작은 동전부터 더해본다
Copy link
Contributor

Choose a reason for hiding this comment

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

지금의 0-1 BFS에서 정렬은 이점이 없어 보이고 생략 가능합니다.

int tempAmount = currentAmount + coin;

if (tempAmount == amount) return coinCount; // 최소 동전 개수 찾음

Copy link
Contributor

@yhkee0404 yhkee0404 Aug 16, 2025

Choose a reason for hiding this comment

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

가지치기를 위해 (#1699 (comment)) 정렬 상태를 이용할 수는 있겠네요.

Suggested change
if (tempAmount > amount) break; // Pruning

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

Successfully merging this pull request may close these issues.

2 participants