-
-
Notifications
You must be signed in to change notification settings - Fork 195
[JisooPyo] Week 1 #643
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
[JisooPyo] Week 1 #643
Conversation
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.
전반적으로 시간복잡도와 공간복잡도를 이해하기 쉽게 분석해주시고, 또한 개선점을 추가해서 풀어주셔서 좋았습니다.
* 개선된 버전: 우선순위 큐를 사용 | ||
* | ||
* Runtime: 19 ms(Beats: 96.30 %) | ||
* Time Complexity: O(n log n) |
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.
PriorityQueue에 n개를 삽입하고 k개를 꺼냈으니 O(n log 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.
@EgonD3V 리뷰 감사합니다!
- k개를 꺼내기 전 PriorityQueue에 값들을 삽입 연산을 생각해보면,
- nums에서 고유한 값의 개수가 m개라고 가정합니다.
- offer()의 시간복잡도는 O(log m), 따라서 m개를 삽입하니 O(m log m) 의 시간복잡도로 계산할 수 있습니다.
- k개의 요소를 추출할 때의 시간 복잡도를 생각해보면,
- 각 추출 연산 역시 O(log m)이 되며, k번 수행하게 되므로 O(k log m)이 됩니다.
따라서 전체 시간 복잡도는
- 배열 순회, 그룹화: O(n)
- 우선순위 큐 연산: O(m log m + k log m)
- 최종: O(n + m log m)
여기서 m은 최대 n이 될 수 있으므로 O(n log n)이 됩니다!
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.
아! PriorityQueue의 크기를 k로 제한하면 O(n log 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.
@JisooPyo 님의 답안 코드에서 알고리즘 고수의 향기를 느끼고 갑니다 :) 멋진 활동 부탁드립니다!
var currentNum = num | ||
var currentLength = 0 | ||
|
||
while (numsSet.contains(currentNum)) { |
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.
요렇게 하면 currentNum
변수를 사용할 필요가 없어져서 코드가 더 간단해지지 않을까 생각해보았습니다. (안 중요)
while (numsSet.contains(currentNum)) { | |
while (numsSet.contains(num + currentLength)) { |
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.
@DaleSeo 오 그렇네요! 리뷰 감사합니다!!
(ps. 안 중요 -> 중요)
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.