-
-
Notifications
You must be signed in to change notification settings - Fork 339
[gyeo-ri] WEEK 03 solutions #2452
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 all commits
Commits
Show all changes
4 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,36 @@ | ||
| """ | ||
| [결과 요약] | ||
| # 재시도횟수: 2회 | ||
| 1. 반복문으로 구하기: 로직은 쉬우나 n이 최대 10^4이므로 input 길이에 따라 속도가 느려질 수 있음 | ||
| 2. 등차수열의 원리를 활용하여 n!을 구한 다음 nums의 합을 빼주기 | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def missingNumber(self, nums: list[int]) -> int: | ||
| # (Runtime: 3ms / Beats 61.16%, Memory 20.23MB / Beats: 98.53% ) | ||
| len_nums = len(nums) | ||
| total_sum = int(len_nums * (len_nums + 1) / 2) | ||
| return total_sum - sum(nums) | ||
|
|
||
| """ | ||
| 이때, 아래와 같이 변수 선언 없이 계산하면(In Memory) 메모리는 조금 더 쓰지만 속도는 빨라진다. | ||
| (Runtime: 0ms / Beats 100.00%, Memory 20.34MB / Beats: 90.41% ) | ||
| return int(len(nums) * (len(nums) + 1) / 2) - sum(nums) | ||
| """ | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_cases = [ | ||
| ([3, 0, 1], 2), | ||
| ([0, 1], 2), | ||
| ([9, 6, 4, 2, 3, 5, 7, 0, 1], 8), | ||
| ] | ||
|
|
||
| solution = Solution() | ||
| for idx, case_ in enumerate(test_cases): | ||
| nums, answer = case_ | ||
| result = solution.missingNumber(nums) | ||
| assert ( | ||
| answer == result | ||
| ), f"Test Case {idx} Failed: Expected {answer}, Got {result}" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.