Skip to content

[daehyun99] WEEK 04 Solutions - #2750

Merged
daehyun99 merged 4 commits into
DaleStudy:mainfrom
daehyun99:W4
Jul 18, 2026
Merged

[daehyun99] WEEK 04 Solutions#2750
daehyun99 merged 4 commits into
DaleStudy:mainfrom
daehyun99:W4

Conversation

@daehyun99

@daehyun99 daehyun99 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn 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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search, Two Pointers
  • 설명: 코드는 회전된 정렬 배열에서 최소값을 찾기 위해 양끝 비교와 요소 제거/교환으로 두 포인터의 위치를 좁히는 방식에 가깝습니다. 최적화 기법이나 패턴은 명확히 이 코드에서 두 포인터의 협력으로 구간을 좁히는 흐름으로 해석될 수 있습니다.

📊 시간/공간 복잡도 분석

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

피드백: 입력 배열의 회전 여부를 판단한 뒤 최소값을 찾는 간단한 구현이지만, 최악의 경우 전체 원소를 확인하므로 시간 복잡도는 선형이다.

개선 제안: 이진탐색으로 회전점을 이용해 O(log n) 시간으로 최솟값을 찾으면 더 효율적이다. 불필요한 pop/assignment 대신 인덱스 범위를 관리해 구현하는 편이 좋다.

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

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.

You must write an algorithm that runs in O(log n) time.
이 문제는 Log N 시간복잡도로 답을 제한하고 있기 때문에 꼭 이진탐색으로 한번 시도해 보시길 바래요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You must write an algorithm that runs in O(log n) time. 이 문제는 Log N 시간복잡도로 답을 제한하고 있기 때문에 꼭 이진탐색으로 한번 시도해 보시길 바래요

헉 그렇군요. O(N)으로 풀어서 나름 뿌듯했는데 더 개선할 수 있었군요!
개선해봤습니다!

class Solution:
    def findMin(self, nums: List[int]) -> int:
        left_idx = 0
        mid_idx = len(nums)//2
        right_idx = len(nums)-1

        while True:
            if right_idx - left_idx <= 1:
                if nums[left_idx] <= nums[right_idx]:
                    return nums[left_idx]
                else:
                    return nums[right_idx]
            elif nums[mid_idx] > nums[right_idx]:
                left_idx = mid_idx
                mid_idx = (left_idx + right_idx) // 2
                right_idx = right_idx
            else:
                left_idx = left_idx
                right_idx = mid_idx
                mid_idx = (left_idx + right_idx) // 2

@dalestudy

dalestudy Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

📊 daehyun99 님의 학습 현황

이번 주 제출 문제

문제 난이도 유형 분석
find-minimum-in-rotated-sorted-array Medium ⚠️ 유형 불일치
maximum-depth-of-binary-tree Easy ✅ 의도한 유형
merge-two-sorted-lists Easy ⚠️ 유형 불일치
word-search Medium ⚠️ 유형 불일치

누적 학습 요약

  • 풀이한 문제: 15 / 75개
  • 이번 주 유형 일치율: 25% (4문제 중 1문제 일치)

문제 풀이 현황

카테고리 진행도 완료
Array ■■■■□□□ 5 / 10 (Medium 3, Easy 2)
Dynamic Programming ■■■□□□□ 4 / 11 (Easy 1, Medium 3)
Heap ■■□□□□□ 1 / 3 (Medium 1)
Binary ■□□□□□□ 1 / 5 (Easy 1)
String ■□□□□□□ 2 / 10 (Easy 2)
Graph ■□□□□□□ 1 / 8 (Medium 1)
Tree ■□□□□□□ 1 / 14 (Medium 1)
Interval □□□□□□□ 0 / 5 ← 아직 시작 안 함
Linked List □□□□□□□ 0 / 6 ← 아직 시작 안 함
Matrix □□□□□□□ 0 / 4 ← 아직 시작 안 함

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

🔢 API 사용량 (gpt-5-nano)
요청 입력 토큰 출력 토큰 합계 비용
1 1,810 188 1,998 $0.000166

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Depth-First Search, Breadth-First Search, Binary Search
  • 설명: 코드가 트리의 최대 깊이를 구하기 위해 두 가지 방식으로 노드를 탐색하는데, 스택을 이용한 깊이 우선 탐색(while로 구성된 DFS)과 레벨 순서 탐색처럼 보이는 두 번째 구성은 BFS의 아이디어를 사용한다. 트리 순회와 깊이 계산의 패턴이 핵심이다.

📊 시간/공간 복잡도 분석

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

피드백: 스택을 이용한 레벨별 방문으로 깊이를 측정한다. 노드 하나씩만 방문하므로 선형 시간과 스택 공간이 필요하다.

개선 제안: 재귀 DFS나 단일 스택으로도 구현 가능하며, 초기 예외 처리와 반복 종료 조건을 명확히 정리하면 가독성이 좋아진다.

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

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.

1번 답안 level order search 방식으로 하신거 같은데 deque 쓰시면 하나만 쓰셔도 구현 가능하세요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

코멘트보고 다시 풀어봤는데, 하나만 있어도 되네요
리뷰 감사합니다!

from collections import deque
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        que = deque()

        if root is None:
            return 0
        que.append([root, 1])

        while len(que) > 0 :
            node, count = que.popleft()
            if node.left is not None:
                que.append([node.left, count+1])
            if node.right is not None:
                que.append([node.right, count+1])
        return count

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Merge
  • 설명: 두 정렬 리스트를 하나로 합치는 과정에서 포인터를 이용해 두 리스트를 동시에 탐색하며 작은 값을 차례로 연결합니다. 순차적 병합과 포인터 이동으로 시간복잡도 O(n)로 해결하는 전형적인 패턴입니다.

📊 시간/공간 복잡도 분석

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

피드백: 새 노드를 매번 생성하는 방식으로 단순하지만, 원래 리스트의 노드를 재활용하면 추가 공간을 줄일 수 있다.

개선 제안: 가능하면 기존 노드를 재링크하여 메모리 재활용을 통해 추가 공간을 줄이는 방향을 고려해 보자.

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

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.

이 문제는 노드를 다시 만들 필요 없이 재활용이 가능한 문제라 꼭 해보시면 좋을것 같네요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

단순히 ListNode(list2.val) 처럼 넣던 값을 list2로 바꾸면 해결이 되네요.
리뷰 감사합니다!

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        # Space: O(N)
        # Time: O(N)
        dummy = ListNode(val=0, next=None)
        pointer = dummy
        while (list1 is not None) and (list2 is not None):
            if list1.val <= list2.val:
                pointer.next = list1
                list1 = list1.next
            else:
                pointer.next = list2
                list2 = list2.next
            pointer = pointer.next
        if list1 is not None:
            pointer.next = list1
        elif list2 is not None:
            pointer.next = list2

        return dummy.next

Comment thread word-search/daehyun99.py

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Depth-First Search, Breadth-First Search, Backtracking
  • 설명: word-search 문제에서 보드의 각 위치에서 단어의 각 글자를 탐색하는 방식이 DFS/BFS 혼합으로 구현되며, 경로를 추적하며 가능한 위치를 확장하는 방식이 Backtracking의 특징을 보입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(R * C * 4^L)
Space O(L)

피드백: 현재 구현은 각 글자 위치를 추적하고 가능한 경로를 확장하는 방식으로 동작한다. 비효율적인 경우가 있어 최악의 경우 성능이 떨어질 수 있다.

개선 제안: 백트래킹으로 중복 방문을 방지하고, 방문 표시를 보존하면서 재귀적 DFS를 사용하는 것이 일반적이다. 또한 보드 크기와 단어 길이에 따른 최적화도 고려하자.

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

@JAEKWANG97

Copy link
Copy Markdown
Contributor

이번 주는 현재 제출하신 네 문제로 마무리하시는 걸까요!

@daehyun99

Copy link
Copy Markdown
Contributor Author

이번 주는 현재 제출하신 네 문제로 마무리하시는 걸까요!

@JAEKWANG97
앗 넵! 마지막 문제는 2시간 정도 끙끙대며 풀다가 결국 못 풀어서 나중에 다시 풀어보려고 합니다!
리뷰 부탁드려요!

@daehyun99 daehyun99 moved this from Solving to In Review in 리트코드 스터디 8기 Jul 18, 2026

@JAEKWANG97 JAEKWANG97 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.

전체적으로 잘 봤습니다! 특히 Word Search를 문자별 좌표와 가능한 경로를 확장하는 방식으로 푼 점이 제가 사용한 DFS 방식과 달라서 흥미로웠습니다. 좌표 조회에 set을 활용할 수 있는 부분만 가볍게 코멘트 남겼습니다.

문제 풀이하시느라 수고하셨습니다!

Comment thread word-search/daehyun99.py
Comment on lines +4 to +13
pos = defaultdict(list)
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] in word:
tmp = pos.get(board[i][j], [])
tmp.append([i, j])
pos[board[i][j]] = tmp

paths = pos.get(word[0], [])
paths = [[path] for path in paths]

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.

문자별 좌표를 미리 모아두는 흐름이 이해하기 좋았습니다! 저도 나중에 이렇게 접근해야겠어요 ㅎㅎ

pos = defaultdict(set)
pos[board[i][j]].add((i, j))

이렇게 바꾸면 조회 성능이 더 좋아질 것 같아요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

오오.. dict() 내부에 set을 쓸 생각은 못 했었는데 감사합니다.
풀어보니 확실히 성능이 좋아지긴 하네요!
리뷰 감사합니다!

Runtime: 9700 ms -> 5667 ms
Memory: 405 MB -> 371 MB

from collections import defaultdict
class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        pos = defaultdict(set)
        for i in range(len(board)):
            for j in range(len(board[0])):
                if board[i][j] in word:
                    tmp = pos.get(board[i][j], set())
                    tmp.add((i, j))
                    pos[board[i][j]] = tmp

        paths = pos.get(word[0], [])
        paths = [[path] for path in paths]

        for w in word[1:]:
            temp = []

            while len(paths) > 0:
                path = paths.pop()
                i , j = path[-1]
                if ((i, j+1) in pos[w]) and ((i, j+1) not in path):
                    temp.append(path[:] + [(i, j+1)])
                if ((i, j-1) in pos[w]) and ((i, j-1) not in path):
                    temp.append(path[:] + [(i, j-1)])
                if ((i+1, j) in pos[w]) and ((i+1, j) not in path):
                    temp.append(path[:] + [(i+1, j)])
                if ((i-1, j) in pos[w]) and ((i-1, j) not in path):
                    temp.append(path[:] + [(i-1, j)])
            paths.extend(temp)
        for path in paths:
            if len(path) == len(word):
                print(path)
                return True
        return False 

@daehyun99
daehyun99 merged commit c67b9ac into DaleStudy:main Jul 18, 2026
3 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Completed in 리트코드 스터디 8기 Jul 18, 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.

3 participants