-
-
Notifications
You must be signed in to change notification settings - Fork 246
[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
base: main
Are you sure you want to change the base?
Conversation
for (int n : nums) { | ||
map.put(n, map.getOrDefault(n, 0) + 1); | ||
} | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
최소 힙으로 빈도수를 기준으로 정렬해서 top K만 유지하는 방식, 너무 인상 깊었어요!
저는 단순 정렬로 풀 생각만 했는데 힙을 활용하니까 더 효율적인 풀이가 가능하네요. 한 수 배워갑니다 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
감사합니다. 처음에 k개를 항상 유지해야 하나? 생각이 들어서 없이도 시도해봤는데 요소가 증가할 수록 공간 복잡도도 증가하고 요소가 추가될 때마다 정렬하는 요소의 갯수도 증가하므로 성능을 위해서 k개를 유지해야 하는 것 같습니다.
또한 k개를 유지하고 나머지를 버리지 않으면 정답이 나오지 않더라고요:)
int prev2AndNowRob = (i - 2 < 0 ? 0 : dp[i - 2]) + nums[i]; | ||
int prev1Rob = dp[i - 1]; | ||
|
||
dp[i] = Math.max(prev2AndNowRob, prev1Rob); |
There was a problem hiding this comment.
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의 변수 두 개로 공간 복잡도 최적화를 더 해볼 수 있을 것 같습니다!
첫 주 고생 많으셨습니다~! 🚀
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!