-
-
Notifications
You must be signed in to change notification settings - Fork 335
[Cyjin-jani] WEEK 11 Solutions #2599
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
1
commit 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.
+29
−0
Open
Changes from all commits
Commits
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,29 @@ | ||
| /** | ||
| * | ||
| * SC: O(n) | ||
| * TC: O(n) | ||
| */ | ||
| const missingNumber = function (nums) { | ||
| const n = nums.length; | ||
| const range_min = 0; | ||
| const range_max = n; | ||
| const numsMap = new Set(); | ||
|
|
||
| for (let num of nums) { | ||
| numsMap.add(num); | ||
| } | ||
|
|
||
| for (let i = range_min; i <= range_max; i++) { | ||
| if (!numsMap.has(i)) return i; | ||
| } | ||
| }; | ||
|
|
||
| // 위 풀이의 경우 공간복잡도가 O(1)이 되지 않음. | ||
| // 아래의 경우가 공간복잡도 O(1)이 되는 풀이. | ||
| // 가우스 합 공식 이용. n개의 숫자에서 0부터 n까지의 합에서 실제 배열의 합을 빼면 누락된 숫자가 나옴. | ||
| const missingNumber_v2 = function (nums) { | ||
| const n = nums.length; | ||
| const expectedSum = (n * (n + 1)) / 2; | ||
| const actualSum = nums.reduce((acc, num) => acc + num, 0); | ||
| return expectedSum - actualSum; | ||
| }; |
Oops, something went wrong.
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
missingNumber— Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)피드백: 배열의 모든 원소를 Set에 저장하는 데 O(n) 공간이 필요하며, 이후 범위 내에서 존재 여부를 검사하는 과정도 O(n) 시간에 수행됩니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
missingNumber_v2— Time: O(n) / Space: ✅ O(1) → O(1)피드백: 배열의 합을 계산하는 데 O(n) 시간과 상수 공간이 필요하며, 공식 계산은 O(1)입니다.
개선 제안: 현재 구현이 적절해 보입니다.