diff --git a/binary-tree-level-order-traversal/KwonNayeon.py b/binary-tree-level-order-traversal/KwonNayeon.py new file mode 100644 index 000000000..431548843 --- /dev/null +++ b/binary-tree-level-order-traversal/KwonNayeon.py @@ -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 diff --git a/counting-bits/KwonNayeon.py b/counting-bits/KwonNayeon.py new file mode 100644 index 000000000..3739406eb --- /dev/null +++ b/counting-bits/KwonNayeon.py @@ -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 diff --git a/house-robber-ii/KwonNayeon.py b/house-robber-ii/KwonNayeon.py new file mode 100644 index 000000000..7492587a4 --- /dev/null +++ b/house-robber-ii/KwonNayeon.py @@ -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])) diff --git a/meeting-rooms-ii/KwonNayeon.py b/meeting-rooms-ii/KwonNayeon.py new file mode 100644 index 000000000..202809a58 --- /dev/null +++ b/meeting-rooms-ii/KwonNayeon.py @@ -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) +