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
22 changes: 22 additions & 0 deletions non-overlapping-intervals/okyungjin.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.

🏷️ 알고리즘 패턴 분석

non-overlapping-intervals/okyungjin.py
"""
https://leetcode.com/problems/non-overlapping-intervals/

Time: O(N)
Space: O(1)
"""
class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        # 1. end 오름차순 intervals 정렬
        intervals.sort(key=lambda x: x[1])

        # remove count
        count = 0        
        last_end = float('-inf')

        for start, end in intervals:
            if start >= last_end: # 구간 안 겹침
                last_end = end
            else: # 구간 겹침
                count += 1
            
        return count
  • 패턴: Greedy, Two Pointers
  • 설명: 최소 제거 수를 구하기 위해 종료시간 기준으로 오름차순 정렬하고, 현재 구간의 시작과 이전 종료를 비교하여 겹침 여부를 판단하는 방식으로 최적해를 찾으므로 Greedy 패턴에 속하며 투 포인터처럼 앞쪽 포인터(last_end)와 현재 포인터를 사용해 유효 구간을 검사합니다.

📊 시간/공간 복잡도 분석

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

피드백: 끝점을 기준으로 정렬한 뒤, 현재 구간의 시작이 last_end 이상이면 겹치지 않는 구간으로 간주하고, 그렇지 않으면 제거 카운트를 증가시키는 방식이다.

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

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

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.

🏷️ 알고리즘 패턴 분석

non-overlapping-intervals/okyungjin.py
"""
https://leetcode.com/problems/non-overlapping-intervals/

Time: O(N log N), intervals 정렬
Space: O(1)
"""
class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        # end 오름차순 intervals 정렬
        intervals.sort(key=lambda x: x[1])

        # remove count
        count = 0        
        last_end = float('-inf')

        for start, end in intervals:
            if start >= last_end: # 구간 안 겹침
                last_end = end
            else: # 구간 겹침
                count += 1
            
        return count
  • 패턴: Greedy, Two Pointers
  • 설명: 끝 점 오름차순 정렬 후 현재 구간과 비교하여 겹치면 제거(카운트)하는 방식으로 최적 해를 찾으므로 그리디 패턴이며, 포인터를 이용한 탐색 흐름도 존재합니다.

📊 시간/공간 복잡도 분석

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

피드백: 종료점으로 정렬한 뒤 현재 구간과의 겹침 여부를 판단하고 겹치면 제거 카운트를 증가시킨다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
https://leetcode.com/problems/non-overlapping-intervals/

Time: O(N log N), intervals 정렬
Space: O(1)
"""
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# end 오름차순 intervals 정렬
intervals.sort(key=lambda x: x[1])

# remove count
count = 0
last_end = float('-inf')

for start, end in intervals:
if start >= last_end: # 구간 안 겹침
last_end = end
else: # 구간 겹침
count += 1

return count
35 changes: 35 additions & 0 deletions same-tree/okyungjin.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.

🏷️ 알고리즘 패턴 분석

same-tree/okyungjin.py
"""
https://leetcode.com/problems/same-tree/description/

N: min(p노드수, q노드수)
Time: O(N)
Space: 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        queue = deque([(p, q)])

        while queue:
            node_p, node_q = queue.popleft()

            if not node_p and not node_q:
                continue

            elif node_p and node_q:
                if node_p.val == node_q.val: 
                    queue.append((node_p.left, node_q.left))
                    queue.append((node_p.right, node_q.right))
                else:
                    return False
            
            else:
                return False

        return True
  • 패턴: Breadth-First Search, Hash Map / Hash Set
  • 설명: 두 트리의 같은 위치 노드를 한 쌍으로 큐에 담아 레벨 단위로 비교하는 BFS 방식으로 모든 노드를 순회합니다. 각 노드의 값과 존재 여부를 비교해 동일하면 자식 노드를 큐에 추가합니다.

📊 시간/공간 복잡도 분석

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

피드백: 큐에 노드를 쌍으로 담아 동시 탐색하며 각 위치의 값과 구조를 검사한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
https://leetcode.com/problems/same-tree/description/

N: min(p노드수, q노드수)
Time: O(N)
Space: 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
queue = deque([(p, q)])

while queue:
node_p, node_q = queue.popleft()

if not node_p and not node_q:
continue

elif node_p and node_q:
if node_p.val == node_q.val:
queue.append((node_p.left, node_q.left))
queue.append((node_p.right, node_q.right))
else:
return False

else:
return False

return True
Loading