Skip to content

[dolphinflow86] WEEK 13 Solutions - #2864

Merged
dalestudy[bot] merged 6 commits into
DaleStudy:mainfrom
dolphinflow86:main
Sep 20, 2026
Merged

dalestudy[bot] merged 6 commits into
DaleStudy:mainfrom
dolphinflow86:main

Conversation

@dolphinflow86

@dolphinflow86 dolphinflow86 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 Status를 In Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

find-median-from-data-stream/dolphinflow86.py
# N is the number of elements added to the data stream.
# TC: O(log N) for addNum, O(1) for findMedian
# SC: O(N) - stores elements split into two heaps

import heapq


class MedianFinder:

    def __init__(self):
        self.small = []  # max-heap (store negative values)
        self.large = []  # min-heap

    def addNum(self, num: int) -> None:
        heapq.heappush(self.small, -num)

        if self.small and self.large and (-self.small[0] > self.large[0]):
            val = -heapq.heappop(self.small)
            heapq.heappush(self.large, val)

        if len(self.small) > len(self.large) + 1:
            val = -heapq.heappop(self.small)
            heapq.heappush(self.large, val)

        if len(self.large) > len(self.small):
            val = heapq.heappop(self.large)
            heapq.heappush(self.small, -val)

    def findMedian(self) -> float:
        if len(self.small) > len(self.large):
            return float(-self.small[0])
        return (-self.small[0] + self.large[0]) / 2.0
  • 패턴: Heap / Priority Queue, Hash Map / Hash Set
  • 설명: 이 문제는 두 개의 힙을 이용해 데이터 스트림에서 중간값을 빠르게 찾는 구조로, 추가 시 힙 간 균형을 맞춰 중앙값을 O(1) 또는 O(log N)으로 반환합니다. 힙을 활용한 데이터 분리와 균형 유지가 핵심 패턴입니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: MedianFinder.addNum — Time: O(log n) / Space: O(n)
복잡도
Time O(log n)
Space O(n)

피드백: 두 개의 힙으로 삽입 시나리오를 균형 있게 유지해 최악의 경우에도 로그 시간에 가운데값을 재조정합니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: MedianFinder.findMedian — Time: O(1) / Space: O(n)
복잡도
Time O(1)
Space O(n)

피드백: 상황에 따라 두 힙의 최댓값/최솟값을 활용해 상호 보완적으로 중간값을 계산합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

insert-interval/dolphinflow86.py
# N is the number of intervals.
# TC: O(N) - single pass through intervals array
# SC: O(N) - stores result intervals list


class Solution:

    def insert(self, intervals, newInterval):
        result = []
        i = 0
        n = len(intervals)

        while i < n and intervals[i][1] < newInterval[0]:
            result.append(intervals[i])
            i += 1

        while i < n and intervals[i][0] <= newInterval[1]:
            newInterval[0] = min(newInterval[0], intervals[i][0])
            newInterval[1] = max(newInterval[1], intervals[i][1])
            i += 1

        result.append(newInterval)

        while i < n:
            result.append(intervals[i])
            i += 1

        return result
  • 패턴: Two Pointers, Greedy, Binary Search
  • 설명: 삽입하려는 구간과 기존 구간 배열을 스캔하며, 겹치지 않는 구간은 유지하고 겹치는 구간은 합쳐 하나의 구간으로 합치는 방식이다. 연속 탐색으로 정렬된 구간을 한 번의 순회로 처리하는 특징이 있다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 왼쪽, 중간, 오른쪽 파트를 순차적으로 처리하여 중복되는 구간을 하나의 구간으로 합칩니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

@dalestudy

dalestudy Bot commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

📊 dolphinflow86 님의 학습 현황

이번 주 제출 문제

문제 난이도 유형 분석
find-median-from-data-stream Hard ✅ 의도한 유형
insert-interval Medium ✅ 의도한 유형
kth-smallest-element-in-a-bst Medium ✅ 의도한 유형
lowest-common-ancestor-of-a-binary-search-tree Medium ✅ 의도한 유형
number-of-connected-components-in-an-undirected-graph Medium ✅ 의도한 유형
serialize-and-deserialize-binary-tree Hard ✅ 의도한 유형

누적 학습 요약

  • 풀이한 문제: 60 / 75개
  • 이번 주 유형 일치율: 100% (6문제 중 6문제 일치)

문제 풀이 현황

카테고리 진행도 완료
Array ■■■■■■■ 10 / 10 (Medium 7, Easy 3)
Linked List ■■■■■■■ 6 / 6 (Easy 3, Hard 1, Medium 2)
Dynamic Programming ■■■■■■□ 10 / 11 (Easy 1, Medium 9)
String ■■■■■■□ 9 / 10 (Medium 5, Hard 1, Easy 3)
Graph ■■■■■■□ 7 / 8 (Hard 1, Medium 6)
Binary ■■■■■■□ 4 / 5 (Easy 3, Medium 1)
Matrix ■■■■■□□ 3 / 4 (Medium 3)
Heap ■■■■■□□ 2 / 3 (Hard 1, Medium 1)
Interval ■■■■□□□ 3 / 5 (Easy 1, Medium 2)
Tree ■■■■□□□ 7 / 14 (Hard 1, Medium 3, Easy 3)

🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다.

🔢 API 사용량 (gpt-5-nano)
요청 입력 토큰 출력 토큰 합계 비용
1 2,084 200 2,284 $0.000184
2 3,000 354 3,354 $0.000292
3 2,151 254 2,405 $0.000209
4 1,996 199 2,195 $0.000179
5 2,606 285 2,891 $0.000244
합계 11,837 1,292 13,129 $0.001109

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

kth-smallest-element-in-a-bst/dolphinflow86.py
# H is the height of the BST, and K is the target rank.
# TC: O(H + K) - in-order traversal stops after visiting K elements
# SC: O(H) - stack memory for in-order traversal recursion/loop

# 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 kthSmallest(self, root, k: int) -> int:
        stack = []
        curr = root

        while curr or stack:
            while curr:
                stack.append(curr)
                curr = curr.left

            curr = stack.pop()
            k -= 1

            if k == 0:
                return curr.val

            curr = curr.right

        return -1
  • 패턴: Binary Search, Monotonic Stack, Hash Map / Hash Set
  • 설명: 이 코드는 이진 탐색 트리의 중위 순회를 이용해 k번째 원소를 찾는다(정렬된 순서를 얻기 위해). 스택을 이용한 비재귀 형태의 중위 순회는 특정 시점에서의 상태를 기록하기 위한 Monotonic Stack의 응용으로 볼 수 있으며, 이진 탐색 트리의 특성에 의존하므로 Binary Search의 맥락도 포함된다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: BST의 성질을 이용해 왼쪽으로 내려가며 순서를 카운트합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

lowest-common-ancestor-of-a-binary-search-tree/dolphinflow86.py
# H is the height of the binary search tree.
# TC: O(H) - traverses down tree height
# SC: O(1) - uses iterative traversal without recursion stack

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution:

    def lowestCommonAncestor(self, root, p, q):
        curr = root

        while curr:
            if p.val < curr.val and q.val < curr.val:
                curr = curr.left
            elif p.val > curr.val and q.val > curr.val:
                curr = curr.right
            else:
                return curr

        return None
  • 패턴: Binary Search, Two Pointers
  • 설명: 이 코드는 이진 탐색 트리에서 두 노드의 공통 조상을 찾기 위해 현재 노드 기준으로 p와 q의 값 비교를 통해 한 방향으로만 이동하는 방식으로 탐색한다. 트리의 높이에 비례하는 시간 복잡도이며, 상호 비교를 통한 목표 위치 탐색이 핵심이다.

📊 시간/공간 복잡도 분석

복잡도
Time O(h)
Space O(1)

피드백: 현재 노드 값을 비교해 좌우로 내려가며 LCA를 찾습니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

validate-binary-search-tree/dolphinflow86.py
# N is the number of nodes in the binary tree.
# TC: O(N) - visits each node at most once
# SC: O(H) - recursion stack space proportional to tree height H

# 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 isValidBST(self, root) -> bool:
        def validate(node, low=float("-inf"), high=float("inf")):
            if not node:
                return True

            if not (low < node.val < high):
                return False

            return validate(node.left, low, node.val) and validate(
                node.right, node.val, high
            )

        return validate(root)
  • 패턴: Binary Search, Depth-First Search, Divide and Conquer
  • 설명: 루트-왼쪽-오른쪽 트리를 재귀적으로 탐색하며 각 노드가 유효한 BST 구간(low, high) 내에 있는지 확인하고, 자식 노드에 대해 구간을 갱신하는 방식으로 문제를 풀이합니다. 각 노드는 한 번만 방문하므로 DFS 방식과 구간 분할(Divide and Conquer) 패턴에 해당합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 각 노드의 범위를 왼쪽/오른쪽 서브트리로 점진적으로 좁혀 검사합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

number-of-connected-components-in-an-undirected-graph/dolphinflow86.py
# n is the number of nodes, and E is the number of edges.
# TC: O(V + E * alpha(V)) - near linear time with path compression Union-Find
# SC: O(V) - parent and rank arrays for Union-Find


class Solution:

    def countComponents(self, n: int, edges) -> int:
        parent = list(range(n))
        rank = [1] * n

        def find(node):
            if parent[node] != node:
                parent[node] = find(parent[node])
            return parent[node]

        def union(n1, n2):
            p1, p2 = find(n1), find(n2)
            if p1 == p2:
                return 0

            if rank[p1] > rank[p2]:
                parent[p2] = p1
                rank[p1] += rank[p2]
            else:
                parent[p1] = p2
                rank[p2] += rank[p1]
            return 1

        components = n
        for u, v in edges:
            components -= union(u, v)

        return components
  • 패턴: Union Find
  • 설명: 그래프의 연결 요소 수를 구하기 위해 서로 다른 정점을 하나의 집합으로 합치는 유니온-파인드(Union Find) 구조를 사용합니다. 간선 하나당 합치고, 성공적인 합치면 구성 요소 수를 감소시키는 방식이 특징입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n + m)
Space O(n)

피드백: 가용한 모든 노드를 하나의 집합으로 시작하고, 간선을 따라 서로 다른 집합을 합치며 남은 서로 다른 루트의 개수를 반환합니다. 경로 압축과 랭크를 사용해 실질적 상수 계수도 낮습니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

serialize-and-deserialize-binary-tree/dolphinflow86.py
# N is the number of nodes in the binary tree.
# TC: O(N) - visits each node once during serialization and deserialization
# SC: O(N) - stores node values and recursion stack

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Codec:

    def serialize(self, root):
        vals = []

        def dfs(node):
            if not node:
                vals.append("N")
                return
            vals.append(str(node.val))
            dfs(node.left)
            dfs(node.right)

        dfs(root)
        return ",".join(vals)

    def deserialize(self, data):
        vals = data.split(",")
        self.i = 0

        def dfs():
            if vals[self.i] == "N":
                self.i += 1
                return None

            node = TreeNode(int(vals[self.i]))
            self.i += 1
            node.left = dfs()
            node.right = dfs()
            return node

        return dfs()
  • 패턴: Depth-First Search, Divide and Conquer
  • 설명: 왔: 트리의 좌우를 깊이 우선으로 방문하며 직렬화/역직렬화를 재귀로 처리합니다. DFS를 이용한 순회와 순서를 이용한 분할 구성으로 구현되어 있습니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Codec.serialize — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: DFS로 전체 트리를 순회하며 각 노드 값을 기록하고, null은 N으로 표시합니다. 직렬화 문자열의 길이는 노드 수에 비례합니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Codec.deserialize — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 입력 순서를 따라 재귀적으로 좌우 자식을 재생성합니다. 데이터 전체를 한 번 순회합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

@dolphinflow86
dolphinflow86 force-pushed the main branch 2 times, most recently from 19a1997 to 4aafa96 Compare September 19, 2026 11:09

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

meeting-rooms/dolphinflow86.py
# N is the number of intervals.
# TC: O(N log N) - sorting the intervals by start time
# SC: O(1) - uses constant extra space


class Solution:

    def canAttendMeetings(self, intervals: list[list[int]]) -> bool:
        intervals.sort(key=lambda x: x[0])

        for i in range(1, len(intervals)):
            if intervals[i][0] < intervals[i - 1][1]:
                return False

        return True
  • 패턴: Sorting, Greedy, Binary Search
  • 설명: 주어진 코드는 회의 시작 시점으로 정렬한 뒤 이전 회의의 종료 시간과 현재 회의 시작 시간을 비교하여 겹침을 확인합니다. 정렬을 활용해 간단히 선형 탐색으로 조건을 판단하는 패턴이며, 최적의 자원 배치를 찾는 관점에서 Greedy 성격도 보입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n log n)
Space O(1)

피드백: 정렬에 의해 시간 복잡도가 결정되며, 추가 공간은 상수로 판단된다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

@parkhojeong parkhojeong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이번 주도 수고하셨습니다. 이전 주차 풀이가 포함되어 있는데 제거 부탁드립니다~

@dalestudy dalestudy Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

현재 주차가 종료되어 자동으로 승인되었습니다. PR을 병합해주세요!

@dalestudy
dalestudy Bot merged commit 8deb424 into DaleStudy:main Sep 20, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In Review to Completed in 리트코드 스터디 8기 Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Completed

Development

Successfully merging this pull request may close these issues.

2 participants