-
-
Notifications
You must be signed in to change notification settings - Fork 331
[Cyjin-jani] WEEK 01 solutions #2369
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
Open
Cyjin-jani
wants to merge
3
commits into
DaleStudy:main
Choose a base branch
from
Cyjin-jani:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,4 @@ | ||
| const containsDuplicate = function (nums) { | ||
| const data = new Set(nums); | ||
| return data.size !== nums.length; | ||
| }; |
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,16 @@ | ||
| const topKFrequent = function (nums, k) { | ||
| const tempArr = Array.from({ length: nums.length }, () => []); | ||
| const obj = {}; | ||
|
|
||
| for (let num of nums) { | ||
| obj[num] = (obj[num] || 0) + 1; | ||
| } | ||
|
|
||
| for (let key in obj) { | ||
| const val = obj[key] - 1; | ||
| tempArr[val].push(+key); | ||
| } | ||
|
|
||
| const answer = tempArr.flat(); | ||
| return answer.slice(-k); | ||
| }; | ||
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,14 @@ | ||
| const twoSum = function (nums, target) { | ||
| let memo = {}; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| let current = nums[i]; | ||
| let needed = target - current; | ||
|
|
||
| if (needed in memo) { | ||
| return [memo[needed], i]; | ||
| } | ||
|
|
||
| memo[current] = i; | ||
| } | ||
| }; |
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.
안녕하세요 깔끔한 코드 잘 보았습니다!!
flat() 를 사용하는 방식도 좋지만 전체 버킷을 모두 펼치는 대신에 tempArr를 뒤에서부터 순회하면서 answer 에 필요한 k개만 담아 바로 리턴하는 방식도 고려해보시면 좋을 것 같아요.
공간복잡도는 동일한 O(n) 이지만 k가 작은 경우에는 새 배열을 추가로 생성하기보다는 k개의 개수만 모아서 반환하는 방식이 효율적일 수도 있을거 같네요.