Skip to content

[jinvicky] WEEK 01 solutions #1681

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 13 commits into
base: main
Choose a base branch
from
Open

[jinvicky] WEEK 01 solutions #1681

wants to merge 13 commits into from

Conversation

jinvicky
Copy link

@jinvicky jinvicky commented Jul 21, 2025

답안 제출 문제

작성자 체크 리스트

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

검토자 체크 리스트

Important

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

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

@KyoJin-Hwang KyoJin-Hwang self-requested a review July 21, 2025 05:04
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);

Choose a reason for hiding this comment

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

최소 힙으로 빈도수를 기준으로 정렬해서 top K만 유지하는 방식, 너무 인상 깊었어요!
저는 단순 정렬로 풀 생각만 했는데 힙을 활용하니까 더 효율적인 풀이가 가능하네요. 한 수 배워갑니다 😊

Copy link
Author

Choose a reason for hiding this comment

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

감사합니다. 처음에 k개를 항상 유지해야 하나? 생각이 들어서 없이도 시도해봤는데 요소가 증가할 수록 공간 복잡도도 증가하고 요소가 추가될 때마다 정렬하는 요소의 갯수도 증가하므로 성능을 위해서 k개를 유지해야 하는 것 같습니다.
또한 k개를 유지하고 나머지를 버리지 않으면 정답이 나오지 않더라고요:)

@jinvicky jinvicky moved this from Solving to In Review in 리트코드 스터디 5기 Jul 25, 2025
int prev2AndNowRob = (i - 2 < 0 ? 0 : dp[i - 2]) + nums[i];
int prev1Rob = dp[i - 1];

dp[i] = Math.max(prev2AndNowRob, prev1Rob);
Copy link
Contributor

Choose a reason for hiding this comment

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

변수 네이밍이 직관적이어서 로직을 이해하기 정말 쉬웠습니다! 그리고 i == 1 일 때의 예외처리도 꼼꼼하게 해주신 것 같아요 🤩

추가로 풀이의 DP 점화식이 dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])인 것으로 보이는데요, 이때 dp[i - 2]dp[i - 1] 값만 필요하기 때문에 O(n) space의 dp list가 아닌 O(1) space의 변수 두 개로 공간 복잡도 최적화를 더 해볼 수 있을 것 같습니다!

첫 주 고생 많으셨습니다~! 🚀

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

Successfully merging this pull request may close these issues.

3 participants