Skip to content

[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
merged 5 commits into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions find-median-from-data-stream/KwonNayeon.py
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이 익숙하지 않아서 일단 리스트로 문제를 풀었습니다.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 heap 문법이 익숙하지 않아서 찾아보며 했는데 막상 써보면 쉽더라구여.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

소수임을 명시하기 위해 2.0을 쓰신 점 배워갑니다.


11 changes: 9 additions & 2 deletions meeting-rooms/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@

풀이방법:
1. Base case: 빈 배열/none일 때 True 반환
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Expand Down