-
-
Notifications
You must be signed in to change notification settings - Fork 195
[KwonNayeon] Week 14 #1090
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
[KwonNayeon] Week 14 #1090
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b42bcc2
Add placeholder for 'Counting Bits'
KwonNayeon ac0aec7
solve: Counting Bits
KwonNayeon e59c219
clarify binary representation description
KwonNayeon e79bc51
solve: Binary Tree Level Order Traversal
KwonNayeon 8105fcb
solve: House Robber 2
KwonNayeon 59fa4d9
solve: Meeting Rooms 2
KwonNayeon f678055
Edit file name to resolve naming conflict
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,46 @@ | ||
""" | ||
Constraints: | ||
- The number of nodes in the tree is in the range [0, 2000]. | ||
- -1000 <= Node.val <= 1000 | ||
|
||
Time Complexity: O(n) | ||
- 각 노드를 한 번씩만 방문함 | ||
|
||
Space Complexity: O(n) | ||
- 결과 리스트는 모든 노드의 값을 저장함 | ||
|
||
풀이방법: | ||
1. queue와 BFS를 활용하여 레벨 순서로 노드를 순회 | ||
2. 각 레벨의 노드들을 별도의 리스트로 모아서 결과에 추가 | ||
3. 각 노드를 처리할 때 그 노드의 자식들을 큐에 추가하여 다음 레벨로 넘어감 | ||
""" | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
if not root: | ||
return [] | ||
|
||
result = [] | ||
queue = deque([root]) | ||
|
||
while queue: | ||
level_size = len(queue) | ||
current_level = [] | ||
|
||
for _ in range(level_size): | ||
node = queue.popleft() | ||
current_level.append(node.val) | ||
|
||
if node.left: | ||
queue.append(node.left) | ||
if node.right: | ||
queue.append(node.right) | ||
|
||
result.append(current_level) | ||
|
||
return result |
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,33 @@ | ||
""" | ||
Constraints: | ||
- 0 <= n <= 10^5 | ||
|
||
Time Complexity: O(n log n) | ||
- 외부 루프: O(n) (0부터 n까지 반복) | ||
- hammingWeight 함수: O(log n) | ||
- 총 시간 복잡도: O(n) * O(log n) = O(n log n) | ||
|
||
Space Complexity: O(n) | ||
- 결과를 저장하기 위한 길이 n+1의 배열 필요 | ||
|
||
풀이방법: | ||
1. 길이가 n+1인 ans 배열을 생성 | ||
2. 0부터 n까지의 각 숫자에 대해: | ||
- hammingWeight 함수를 사용하여 숫자 i를 이진수로 변환했을 때 1의 개수 계산 | ||
- 결과를 ans[i]에 저장 | ||
3. ans 배열 반환 | ||
""" | ||
class Solution: | ||
def countBits(self, n: int) -> List[int]: | ||
ans = [0] * (n+1) | ||
|
||
for i in range(n+1): | ||
ans[i] = self.hammingWeight(i) | ||
return ans | ||
|
||
def hammingWeight(self, n: int) -> int: | ||
count = 0 | ||
while n: | ||
count += n & 1 | ||
n >>= 1 | ||
return count |
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,38 @@ | ||
""" | ||
Constraints: | ||
- 1 <= nums.length <= 100 | ||
- 0 <= nums[i] <= 1000 | ||
|
||
Time Complexity: O(n) | ||
|
||
Space Complexity: O(n) | ||
|
||
풀이방법: | ||
1. 집이 하나만 있는 경우 → 그 집을 텀 | ||
2. 먼저 원형이 아닌 일반적인 House Robber 문제를 해결하는 함수를 구현함 | ||
3. 이 문제의 제약조건을 포함하기 위해: | ||
- 첫 번째 집을 털지 않는 경우 (nums[1:]) | ||
- 마지막 집을 털지 않는 경우 (nums[:-1]) | ||
- 둘 중 최댓값을 반환 | ||
""" | ||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
def rob_simple(houses): | ||
if len(houses) == 1: | ||
return houses[0] | ||
elif len(houses) == 2: | ||
return max(houses[0], houses[1]) | ||
|
||
dp = [0] * len(houses) | ||
dp[0] = houses[0] | ||
dp[1] = max(houses[0], houses[1]) | ||
|
||
for i in range(2, len(houses)): | ||
dp[i] = max(dp[i-1], houses[i] + dp[i-2]) | ||
|
||
return dp[-1] | ||
|
||
return max(rob_simple(nums[1:]), rob_simple(nums[:-1])) |
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,34 @@ | ||
""" | ||
Time Complexity: O(n log n) | ||
- 정렬에 O(n log n) | ||
- 각 미팅에 대한 힙 연산이 O(log n) | ||
|
||
Space Complexity: O(n) | ||
- 최악의 경우 모든 미팅이 동시에 진행되어 힙에 n개의 원소가 저장됨 | ||
|
||
풀이방법: | ||
1. 미팅 시간(intervals)을 시작 시간을 기준으로 정렬함 | ||
2. 최소 힙을 이용해서 현재 진행 중인 미팅의 종료시간을 저장함 | ||
3. 각 미팅을 순회하면서: | ||
- 새 미팅의 시작시간이 힙의 최소 종료시간보다 크거나 같으면 -> 가장 일찍 끝나는 미팅을 힙에서 제거 | ||
- 현재 미팅의 종료시간을 힙에 추가 | ||
4. 힙의 크기 = 필요한 최소 회의실의 수 | ||
""" | ||
import heapq | ||
|
||
def min_meeting_rooms(intervals): | ||
if not intervals: | ||
return 0 | ||
|
||
intervals.sort(key=lambda x: x[0]) | ||
|
||
rooms = [] | ||
|
||
heapq.heappush(rooms, intervals[0][1]) | ||
|
||
for i in range(1, len(intervals)): | ||
if intervals[i][0] >= rooms[0]: | ||
heapq.heappop(rooms) | ||
heapq.heappush(rooms, intervals[i][1]) | ||
return len(rooms) | ||
|
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.
BFS를 사용하여 문제를 풀어주셨네요!
DFS로 풀어보시는 것도 재미있는 도전이 될 것 같습니다.
공간 복잡도를 O(h), h는 재귀스택 만큼의 공간 으로 최적화 할 수 있을것 같아요!
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.
@TonyKim9401 님 리뷰 감사합니다! DFS로도 한 번 풀어봐야겠어요 😄