-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Tessa1217] WEEK 01 solutions #1129
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.
시간, 공간복잡도에 대한 설명이 추가된다면 더 좋을 것 같습니다 ㅎㅎ 고생하셨습니다!
Set<Integer> distincts = new HashSet<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
distincts.add(nums[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.
중복을 찾는 것이 목적이므로, 전체 배열을 다 순회하지 않고 중복 발견 즉시 리턴하는 방식은 어떨까요?
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.
`
public boolean containsDuplicate(int[] nums) {
Set<Integer> distincts = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
boolean added = distincts.add(nums[i]);
if (!added) {
return true;
}
}
return false;
}
`
리뷰해주신대로 개선했더니 확실히 런타임이 줄었네요. 좋은 리뷰 감사합니다!
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (numMap.containsKey(target - nums[i])) { | ||
answer[0] = numMap.get(target - nums[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.
target - nums[i]에 대한 변수를 생성해서 연산한 값을 담아주면 가독성과 연산 최소화가 가능해보입니다!
if (numMap.containsKey(target - nums[i])) { | ||
answer[0] = numMap.get(target - nums[i]); | ||
answer[1] = i; | ||
break; |
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.
배열은 조건 만족 시점에 생성하고 break없이 바로 리턴해도 될것같습니다.
return 0; | ||
} | ||
|
||
Arrays.sort(nums); |
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.
문제 제약조건이 시간복잡도 O(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.
넵 시간복잡도가 정렬 때문에 O(n log n)이 될 것 같기는 한데 일단 문제 풀이 자체는 통과했습니다. 그래도 O(n)으로 시간 복잡도를 줄일 수 있는 풀이를 한번 찾아보는 게 좋겠네요. 좋은 리뷰 감사합니다! 1주차 고생하셨습니다!
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!