-
-
Notifications
You must be signed in to change notification settings - Fork 245
[reach0908] WEEK 01 solutions #1685
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6fdb0fa
solve: contains-duplicate
46057fc
solution: two sum
reach0908 b0fc4e3
solve: top-k-frequent-elements-1
reach0908 8e22356
solve: longest-consecutive-sequence
reach0908 dce519d
solve: longest-consecutive-sequence2 개선 문제 풀이 추가
reach0908 501b925
solve: house-robber 문제 풀이 추가 및 DP 방식으로 개선
reach0908 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,19 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
* 풀이 방법: | ||
* - 주어진 배열을 순회하며 해쉬테이블을 구성하고 해쉬 테이블에 이미 값이 있는 경우 early return 을 통해 중복됨을 반환 | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
const containsDuplicate = function (nums) { | ||
const hashTable = new Set(); | ||
for (let i = 0; i < nums.length; i++) { | ||
if (hashTable.has(nums[i])) { | ||
return true; | ||
} | ||
hashTable.set(nums[i], i); | ||
} | ||
return false; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 가독성있게 잘 하신 것 같습니다 |
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,29 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n log n) | ||
* space complexity: O(n) | ||
* runtime: 3ms | ||
* 풀이 방법: | ||
* 해시맵을 통해 각 숫자의 빈도수를 계산 | ||
* 해시맵을 정렬하여 빈도수가 높은 순서대로 정렬 | ||
* 정렬된 배열을 k만큼 짤라서 반환 | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
const topKFrequent = function (nums, k) { | ||
const hashMap = new Map(); | ||
|
||
for (const num of nums) { | ||
if (hashMap.has(num)) { | ||
hashMap.set(num, hashMap.get(num) + 1); | ||
} else { | ||
hashMap.set(num, 1); | ||
} | ||
} | ||
|
||
return Array.from(hashMap.entries()) | ||
.sort((a, b) => b[1] - a[1]) | ||
.slice(0, k) | ||
.map(([num]) => num); | ||
}; |
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,55 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n^2) | ||
* space complexity: O(1) | ||
* 풀이 방법: | ||
* 카운터를 통해 반복문 돌기 | ||
* 겉으로는 반복문이 한개 같아보이지만 이중반복문이다. | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
const twoSumSoluton1 = function (nums, target) { | ||
let left = 0; | ||
let right = 1; | ||
while (left < nums.length - 1) { | ||
const sum = nums[left] + nums[right]; | ||
if (sum === target) { | ||
return [left, right]; | ||
} | ||
|
||
if (right < nums.length - 1) { | ||
right += 1; | ||
} else { | ||
left += 1; | ||
right = left + 1; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @description | ||
* 다른 사람들의 풀이를 보고 개선한 솔루션 | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
* 풀이 방법: | ||
* 이전 값들 중에 원하는 값이 있는지만 확인 후 추출, 시간복잡도를 크게 감소시킴 | ||
* 하지만 해쉬맵을 만들어야해서 공간복잡도는 O(n)으로 변경 | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
const twoSumSoluton2 = function (nums, target) { | ||
const hashMap = new Map(); | ||
|
||
for (let i = 0; i < nums.length; i += 1) { | ||
const calculatedTarget = target - nums[i]; | ||
if (hashMap.has(calculatedTarget)) { | ||
return [i, hashMap.get(calculatedTarget)]; | ||
} | ||
|
||
hashMap.set(nums[i], i); | ||
} | ||
|
||
return []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 무조건 하나의 유효값이 있다 해서 예외처리를 안했는데, 이렇게 하는 것도 디테일이라고 생각이 드네요. 좋은 것 같습니다 :) |
||
}; |
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.
잘 푸신 것 같습니다