Skip to content
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
29 changes: 29 additions & 0 deletions binary-tree-level-order-traversal/yuseok89.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.

🏷️ 알고리즘 패턴 분석

binary-tree-level-order-traversal/yuseok89.py
# TC: O(N)
# SC: O(N)
# 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: TreeNode | None) -> list[list[int]]:

        ret = []

        def preOrder(root: TreeNode | None, height: int):
            if len(ret) == height:
                ret.append([])

            ret[height].append(root.val)

            if root.left:
                preOrder(root.left, height + 1)
            if root.right:
                preOrder(root.right, height + 1)

        if root:
            preOrder(root, 0)

        return ret
  • 패턴: Depth-First Search, Binary Search, Monotonic Stack, Breadth-First Search, Hash Map / Hash Set, Dynamic Programming, Two Pointers, Sliding Window, Fast & Slow Pointers, Backtracking, Divide and Conquer, Union Find, Trie, Bit Manipulation, Heap / Priority Queue, Greedy
  • 설명: 주어진 코드는 재귀를 이용한 트리의 각 레벨에 값을 모으는 방식으로 구현되었으며, 실제로 깊이 우선 탐색(DFS)으로 각 높이에 따라 결과를 분리하여 저장합니다. 원래 의도는 레벨 순서 순회(BFS)이지만 재귀 구조로 구현되어 DFS 패턴으로 분류됩니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n) ✅
Space O(N) O(n) ✅

피드백: 재귀를 통해 트리를 순회하고 각 높이에 따라 리스트를 확장하며 값을 추가한다. 레벨 순서를 얻으려면 큐를 이용한 BFS가 일반적이지만 현재 구현은 프리오더 순회로 동작한다.

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

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.

엄청 깔끔하게 잘 해결하셨네요!
정말 아주 억지로 트집잡자면 재귀가 트리의 높이만큼 스택이 쌓이니까 Iterative하게 시도해 보는거를 한번 해보시는것도 좋다는 정도려나요?
깔끔한 코드 잘 보고 갑니다!

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.

진짜 회원님 코드 볼때마다 느끼는거지만 Leetcode Study 처음 오셨을때랑 지금이랑 차이가 어마어마하신것 같아서 대단한거 같아요
고생이 많으십니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TC: O(N)
# SC: O(N)
# 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: TreeNode | None) -> list[list[int]]:

ret = []

def preOrder(root: TreeNode | None, height: int):
if len(ret) == height:
ret.append([])

ret[height].append(root.val)

if root.left:
preOrder(root.left, height + 1)
if root.right:
preOrder(root.right, height + 1)

if root:
preOrder(root, 0)

return ret

17 changes: 17 additions & 0 deletions counting-bits/yuseok89.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.

🏷️ 알고리즘 패턴 분석

counting-bits/yuseok89.py
# TC: O(N)
# SC: O(N)
class Solution:
    def countBits(self, n: int) -> list[int]:
        ans = []

        upper = 1
        ans.append(0)

        for idx in range(1, n + 1):
            ans.append(ans[idx - upper] + 1)

            if idx == upper:
                upper *= 2

        return ans
  • 패턴: Dynamic Programming, Two Pointers, Hash Map / Hash Set
  • 설명: 주어진 코드는 이진수에서 각 수의 1의 개수를 구하는 DP 방식의 점화식(전개식)을 활용합니다. 이전 값들을 이용해 현재 값을 계산하고, 상위 블록 경계에서 값을 갱신하는 형태로 최적화됩니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n) ✅
Space O(N) O(n) ✅

피드백: 동적 계획으로 각 수의 비트를 빠르게 계산한다. 상향식으로 이전 값을 활용해 O(n) 시간에 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.

상당히 깔끔하게 잘 해결하셨네요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TC: O(N)
# SC: O(N)
class Solution:
def countBits(self, n: int) -> list[int]:
ans = []

upper = 1
ans.append(0)

for idx in range(1, n + 1):
ans.append(ans[idx - upper] + 1)

if idx == upper:
upper *= 2

return ans

19 changes: 19 additions & 0 deletions house-robber-ii/yuseok89.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.

🏷️ 알고리즘 패턴 분석

house-robber-ii/yuseok89.py
# TC: O(N)
# SC: O(1)
class Solution:
    def rob(self, nums: list[int]) -> int:

        n = len(nums)

        def calc(nums, s, e):
            rob, skip = 0, 0
            for idx in range(s, e):
                rob, skip = skip + nums[idx], max(rob, skip)

            return max(rob, skip)

        if n == 1:
            return nums[0]

        return max(calc(nums, 0, n - 1), calc(nums, 1, n))
  • 패턴: Dynamic Programming, Greedy
  • 설명: 배열을 순차적으로 순회하며 현재까지의 최대 이익을 rob/skip로 관리하는 DP 형태이며, 원형 배열의 첫/마지막 요소 중 무엇을 선택할지 두 경우로 나눠 최댓값을 비교한다. 이는 House Robber II의 최적해를 구하는 전형적 DP 패턴과 분할로 최소한의 중복계산을 사용한 결정 로직을 포함한다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n) ✅
Space O(1) O(1) ✅

피드백: 두 구간으로 나누어 DP를 수행하고, 원소의 개수에 따라 첫 원소 포함/미포함 두 경우를 비교한다. 공간은 상수로 유지된다.

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

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.

오 문제를 함수로 일반화 하신다음에 따로 두가지 경우로 구현하셨네요
그 덕분에 코드가 상당히 가독성이 좋아지는것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TC: O(N)
# SC: O(1)
class Solution:
def rob(self, nums: list[int]) -> int:

n = len(nums)

def calc(nums, s, e):
rob, skip = 0, 0
for idx in range(s, e):
rob, skip = skip + nums[idx], max(rob, skip)

return max(rob, skip)

if n == 1:
return nums[0]

return max(calc(nums, 0, n - 1), calc(nums, 1, n))

Loading