-
-
Notifications
You must be signed in to change notification settings - Fork 245
[seungseung88] WEEK 01 solutions #1136
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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e87952
two sum solution
seungseung88 ae344e8
달래영상 시청 후 수정 two sum#2
seungseung88 2beb39e
first solution Contains Duplicate#1
seungseung88 51cb0c7
top k frequent elements #1
seungseung88 de8a7a0
Longest Consecutive Sequence (풀이 #1): 초안 제출
seungseung88 8f66c84
House Robber 풀이 1 #264
seungseung88 8e4ca06
코드 수정
seungseung88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
// 배열을 한 번 도니 시간 복잡도 O(n) | ||
// 최악의 경우 nums의 크기 배열만큼 증가하므로 공간 복잡도는 O(n) | ||
|
||
const twoSum = (nums, target) => { | ||
// 배열을 한 번 순회 | ||
// {값: 인덱스} 형태로 저장 | ||
const indicies = {}; | ||
|
||
for (let i = 0; i < nums.length; i += 1) { | ||
// target숫자에서 현재 숫자를 뺀 숫자가 존재하는지 확인한다. | ||
const targetIndex = nums.indexOf(target - nums[i]); | ||
// targetIndex가 존재하고(-1이 아님), 현재 숫자와 같은 인덱스 숫자가 아니라면 반환한다. | ||
if (targetIndex !== -1 && i !== targetIndex) { | ||
return [i, targetIndex]; | ||
// 타겟값에서 현재 가리키는 숫자를 뺀 값을 저장 | ||
const complement = target - nums[i]; | ||
|
||
// complement가 indicies안에 존재하면 해당 값을 반환 | ||
if (complement in indicies) { | ||
const j = indicies[complement]; | ||
return [j, i]; | ||
} | ||
// 존재 하지 않으면 값과 인덱스 형태로 저장 ex> { 11: 0, 15: 1, 2: 2 } | ||
indicies[nums[i]] = i; | ||
console.log(indicies); | ||
} | ||
}; | ||
|
||
// 시간 복잡도는 O(n^2) | ||
// 공간 복잡도는 O(1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
객체를 이용해서 푸셨네요!☺️
저는 Map을 통해서 관계를 그렸는데 다양한 풀이법이 있는 것 같네요