-
-
Notifications
You must be signed in to change notification settings - Fork 247
[KwonNayeon] Week 13 solutions #1621
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
+52
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
972490d
Add placeholder for Find Median from Data Stream problem
KwonNayeon fb59771
Merge branch 'DaleStudy:main' into main
KwonNayeon 2606c65
Solve meeting rooms problem
KwonNayeon 02fa577
Solve Find median from data stream
KwonNayeon 8ad0e8d
Add newlines to Find median from data stream problem
KwonNayeon 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,43 @@ | ||
""" | ||
Constraints: | ||
- -10^5 <= num <= 10^5 | ||
- There will be at least one element in the data structure before calling findMedian. | ||
- At most 5 * 10^4 calls will be made to addNum and findMedian. | ||
|
||
Time Complexity: | ||
- addNum(): O(nlogn) | ||
- 매번 정렬하기 때문 | ||
- findMedian(): O(1) | ||
- 정렬된 리스트에서 인덱스 접근 | ||
|
||
Space Complexity: O(n) | ||
- 입력된 모든 숫자를 리스트에 저장 | ||
|
||
풀이방법: | ||
1. 리스트 자료구조 사용 | ||
2. 리스트에 각 요소들 추가 후 정렬 | ||
3. 리스트의 요소 갯수가 홀수/짝수일 때의 경우를 나눠서 median 값을 구함 | ||
|
||
메모: | ||
- heap이 익숙하지 않아서 일단 리스트로 문제를 풀었습니다. | ||
- 나중에 heap으로 다시 풀기 | ||
""" | ||
class MedianFinder: | ||
|
||
def __init__(self): | ||
self.nums = [] | ||
|
||
def addNum(self, num: int) -> None: | ||
self.nums.append(num) | ||
self.nums.sort() | ||
|
||
|
||
def findMedian(self) -> float: | ||
n = len(self.nums) | ||
if n % 2 == 1: | ||
return self.nums[n // 2] | ||
else: | ||
mid1 = self.nums[n // 2 - 1] | ||
mid2 = self.nums[n // 2] | ||
return (mid1 + mid2) / 2.0 | ||
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. 소수임을 명시하기 위해 2.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 |
---|---|---|
|
@@ -13,12 +13,19 @@ | |
|
||
풀이방법: | ||
1. Base case: 빈 배열/none일 때 True 반환 | ||
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. 풀이 방법, 메모 등 정리를 정말 잘하시는거 같아요. 한 문제를 풀어도 기억에 오래 남을거 같은 좋은 방법이네요 잘 보고 갑니다 👍🏻😃 |
||
2. intervals를 시작점 기준으로 정렬 | ||
3. prev_end 초기화 (첫 번째 미팅의 종료 시간) | ||
2. 리스트(ntervals)를 시작점 기준으로 정렬 | ||
3. prev_end(첫 번째 미팅의 종료 시간) 변수 초기화 | ||
4. 두 번째 미팅부터 순회하면서: | ||
- 만약 현재 시작점 (미팅 시작 시간)이 이전 미팅의 종료 시간보다 작으면 false 반환 | ||
- 그렇지 않으면 prev_end를 현재 미팅의 종료 시간으로 업데이트 | ||
5. 모든 미팅을 검사한 후에도 충돌이 없으면 true 반환 | ||
|
||
메모: | ||
1. 변수명 헷갈림 | ||
- intervals는 리스트 -> `.end` 속성 없음 | ||
- interval은 객체 -> `.end` 속성 있음 | ||
2. 정렬 문법 | ||
- list.sort(key=lambda x: x.start) | ||
""" | ||
from typing import List | ||
|
||
|
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.
저도 heap 문법이 익숙하지 않아서 찾아보며 했는데 막상 써보면 쉽더라구여.
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.
저도 heap으로 도전해봐야겠어요! 감사합니당 👍