-
-
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 5 commits
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 시간 복잡도: O(n) => 배열을 한 번만 순회하므로 | ||
// 공간 복잡도: O(n) => 최악의 경우 모든 요소를 객체에 저장하므로 | ||
const containsDuplicate = (nums) => { | ||
const indices = {}; | ||
|
||
// 배열을 한 번 순회 | ||
for (let i = 0; i < nums.length; i += 1) { | ||
const num = nums[i]; | ||
|
||
// 아직 등장하지 않은 숫자라면 객체에 기록 | ||
if (!indices[num]) { | ||
indices[num] = 1; | ||
} else { | ||
// 이미 등장한 숫자라면 중복이므로 true 반환 | ||
return true; | ||
} | ||
} | ||
|
||
// 중복이 없으면 false 반환 | ||
return false; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 시간복잡도: O(n) | ||
* - set.has() => O(1) | ||
* - while() => 최악의 경우 O(n) | ||
* 공간복잡도: new Set() => O(n) | ||
*/ | ||
const longestConsecutive = (nums) => { | ||
// 빈 배열일 때 0 리턴 | ||
if (nums.length === 0) return 0; | ||
|
||
// 중복 제거 | ||
const set = new Set(nums); | ||
|
||
// 연속되는 숫자의 길이를 넣을 변수 설정 | ||
let longest = 0; | ||
|
||
set.forEach((v) => { | ||
// 일단 연속되는 배열에서는 가장 최솟값을 찾음 | ||
if (!set.has(v - 1)) { | ||
let cnt = 1; | ||
|
||
// 연속되는 배열에서 가장 긴 배열을 저장 | ||
while (set.has(v + cnt)) { | ||
cnt += 1; | ||
longest = longest < cnt ? cnt : longest; | ||
} | ||
} | ||
}); | ||
|
||
return longest; | ||
}; |
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||||||||||||||||||
/** | ||||||||||||||||||||||
* 시간 복잡도: O(n log n) | ||||||||||||||||||||||
* - nums 배열 순회하며 빈도수 카운트: O(n) | ||||||||||||||||||||||
* - 빈도수 기준 내림차순 정렬: O(n log n) | ||||||||||||||||||||||
* - 상위 k개 선택: O(k) -> k는 n보다 작으므로 무시 | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* 공간 복잡도: O(n) | ||||||||||||||||||||||
* - countNums 객체: 최악의 경우 모든 숫자가 다른 경우 O(n) | ||||||||||||||||||||||
* - sortedCountNums 배열: countNums와 동일한 크기 O(n) | ||||||||||||||||||||||
* - answer 배열: k 크기이지만 k는 n보다 작으므로 무시 | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
const topKFrequent = (nums, k) => { | ||||||||||||||||||||||
// 각 숫자의 빈도수를 저장하는 객체 | ||||||||||||||||||||||
const countNums = {}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
// nums 배열을 순회하며 각 숫자의 빈도수를 카운트 | ||||||||||||||||||||||
for (let i = 0; i < nums.length; i += 1) { | ||||||||||||||||||||||
const num = nums[i]; | ||||||||||||||||||||||
countNums[num] = !countNums[num] ? 1 : countNums[num] + 1; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// 빈도수를 기준으로 내림차순 정렬 | ||||||||||||||||||||||
const sortedCountNums = Object.entries(countNums).sort((a, b) => b[1] - a[1]); | ||||||||||||||||||||||
const answer = []; | ||||||||||||||||||||||
|
||||||||||||||||||||||
// 상위 k개의 숫자를 answer 배열에 저장 | ||||||||||||||||||||||
for (let i = 0; i < k; i += 1) { | ||||||||||||||||||||||
answer[i] = Number(sortedCountNums[i][0]); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
const answer = []; | |
// 상위 k개의 숫자를 answer 배열에 저장 | |
for (let i = 0; i < k; i += 1) { | |
answer[i] = Number(sortedCountNums[i][0]); | |
} | |
const answer = Array.from(sortedCountNums.entries()) | |
.sort((a, b) => b[1] - a[1]) | |
.slice(0, k) | |
.map(item => item[0]); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 배열을 한 번 도니 시간 복잡도 O(n) | ||
// 최악의 경우 nums의 크기 배열만큼 증가하므로 공간 복잡도는 O(n) | ||
|
||
const twoSum = (nums, target) => { | ||
// {값: 인덱스} 형태로 저장 | ||
const indicies = {}; | ||
|
||
for (let i = 0; i < nums.length; i += 1) { | ||
// 타겟값에서 현재 가리키는 숫자를 뺀 값을 저장 | ||
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); | ||
} | ||
}; |
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.
의도는 충분히 알겠으나 단순히 아래 코드가 가독성이 더 나을수도 있겠다는 생각이 드네요 🤔