Skip to content

ContainsDuplicate #3

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 3 commits into from
Feb 18, 2024
Merged

ContainsDuplicate #3

merged 3 commits into from
Feb 18, 2024

Conversation

yunseorim1116
Copy link
Member

@yunseorim1116 yunseorim1116 commented Feb 17, 2024

ContainsDuplicate

image

객체를 검사하면서 2이상의 숫자가 있으면 바로 true 를 리턴하게 짜도 됐지만

12 : 객체 변환, 14 : 검사
역할을 한줄로 각각 분리하고 싶어서 나누어보았습니다.

@yunseorim1116
Copy link
Member Author

yunseorim1116 commented Feb 17, 2024

Two Sum

image

정석 포문으로 풀어보았습니다.

TwoSum.js Outdated
Comment on lines 8 to 18
for (let i = 0; i < nums.length; i++) {
const x = nums[i];
for (let j = i + 1; j < nums.length; j++) {
const v = nums[j];
const sum = x + v;

if (sum === target) {
answer = [i, j];
break;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

이중 for문으로 풀면 안됩니다. HashMap 개념을 사용해야해요.

Copy link
Member

Choose a reason for hiding this comment

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

이중 for문으로 풀면 시간복잡도가 O(n^2)이 됩니다.

@yunseorim1116
Copy link
Member Author

image
이중포문 => 해쉬맵 객체로 변경해서 다시 풀었습니다.

target(더한 목표 값)에서 현재 반복 돌리고 있는 숫자를 뺀 값을 변수에 담습니다.
로직은 배열의 숫자를 객체로 각각 key(숫자):value(인덱스) 로 저장하고,
다음 반복을 돌 때 마다 key 값을 검사해서 객체 key 에 target - 현재 돌리고 있는 숫자가 존재하는지 검사합니다.

@lukasjhan
Copy link
Member

@yunseorim1116 최고에요 ㅋㅋㅋ

@yunseorim1116 yunseorim1116 merged commit 61197b1 into main Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants