Skip to content

[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

Merged
merged 7 commits into from
Apr 4, 2025
Merged

[Tessa1217] WEEK 01 solutions #1129

merged 7 commits into from
Apr 4, 2025

Conversation

Tessa1217
Copy link
Contributor

@Tessa1217 Tessa1217 commented Mar 30, 2025

답안 제출 문제

작성자 체크 리스트

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

검토자 체크 리스트

Important

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

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

@Tessa1217 Tessa1217 moved this from Solving to In Review in 리트코드 스터디 4기 Apr 3, 2025
Copy link
Contributor

@grapefruitgreentealoe grapefruitgreentealoe left a 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]);
Copy link
Contributor

Choose a reason for hiding this comment

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

중복을 찾는 것이 목적이므로, 전체 배열을 다 순회하지 않고 중복 발견 즉시 리턴하는 방식은 어떨까요?

Copy link
Contributor Author

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]);
Copy link
Contributor

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;
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

문제 제약조건이 시간복잡도 O(n) 까지 허용인데 혹시 통과되셨나요?

Copy link
Contributor Author

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주차 고생하셨습니다!

@Tessa1217 Tessa1217 merged commit 1b178f4 into DaleStudy:main Apr 4, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Review to Completed in 리트코드 스터디 4기 Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

2 participants