-
-
Notifications
You must be signed in to change notification settings - Fork 304
[sujeong-dev] WEEK 01 solutions #2004
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
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.
첫 번째 주 고생 많으셨습니다!! 🚀
추가로 status를 solving에서 in review로 변경해주시면 더 빠르게 리뷰를 받으실 수 있으니 참고해주시면 감사드리겠습니다~!
| * | ||
| * 시간복잡도 계산 | ||
| * Set의 size속성은 입력 크기와 관계없이 일정한 시간이 걸림 | ||
| * => O(1) |
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.
indices.size의 경우 O(1) time이 소요되지만 new Set(nums)에서 내부적으로 nums를 순회하며 원소를 삽입하기 때문에 O(n) time이 소요되므로, 전체 시간복잡도가 O(n)이 될 것 같습니다!
|
|
||
| if (indices.size !== nums.length) return true; | ||
|
|
||
| return false; |
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.
다음과 같이 return 문을 분기 없이 간결하게 나타낼 수도 있을 것 같아요!
return indices.size !== nums.length;| * for loop가 O(n) | ||
| * reduce가 O(n) | ||
| * sort가 O(n log 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.
현재 코드에서는 for 문 내에 find 및 reduce가 위치하므로 전체 시간 복잡도가 O(n^2)이 될 것으로 보입니다.
| * => O(n log n) | ||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| let result = []; |
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.
배열이 아닌 hash map을 이용해서 값 별 frequency를 기록한다면 find로 이미 존재하는 키를 O(n)에 찾을 필요도 없고, reduce로 개수를 매번 O(n)에 계산할 필요가 없으므로 시간 복잡도 최적화가 가능할 것 같습니다!
| */ | ||
| var twoSum = function (nums, target) { | ||
| for (let i = 0; i < nums.length; i++) { | ||
| for (let j = 0; j < nums.length; j++) { |
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.
j = i + 1로 시작한다면 중복 연산도 제거할 수 있고, for 문 내부에서 i === j 비교할 필요도 없을 것 같습니다!
| for (let i = 0; i < nums.length; i++) { | ||
| const findNum = target - nums[i]; | ||
|
|
||
| if (findNum in result && result[findNum] !== i) { |
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.
nums.forEach()로 result hash map을 미리 만들어놓는 방식이 아닌, for 문을 돌며 한 번에 hash map을 점진적으로 만들어나간다면 result[findNum] !== i를 확인할 필요가 없을 것 같습니다.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!