-
-
Notifications
You must be signed in to change notification settings - Fork 359
[dolphinflow86] WEEK 12 Solutions #2857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b1d6583
ab8e69d
3847bcd
a567e02
f434cd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # C is total length of all words, U is number of unique alien letters (<= 26). | ||
| # TC: O(C) - comparing adjacent words and processing topological sort graph | ||
| # SC: O(1) - unique letters and adjacency list bounded by 26 characters | ||
|
|
||
| from collections import deque | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def alienOrder(self, words) -> str: | ||
| adj = {char: set() for word in words for char in word} | ||
| indegree = {char: 0 for word in words for char in word} | ||
|
|
||
| for i in range(len(words) - 1): | ||
| w1, w2 = words[i], words[i + 1] | ||
| min_len = min(len(w1), len(w2)) | ||
|
|
||
| if len(w1) > len(w2) and w1[:min_len] == w2[:min_len]: | ||
| return "" | ||
|
|
||
| for j in range(min_len): | ||
| if w1[j] != w2[j]: | ||
| if w2[j] not in adj[w1[j]]: | ||
| adj[w1[j]].add(w2[j]) | ||
| indegree[w2[j]] += 1 | ||
| break | ||
|
|
||
| queue = deque([char for char in indegree if indegree[char] == 0]) | ||
| result = [] | ||
|
|
||
| while queue: | ||
| char = queue.popleft() | ||
| result.append(char) | ||
|
|
||
| for neighbor in adj[char]: | ||
| indegree[neighbor] -= 1 | ||
| if indegree[neighbor] == 0: | ||
| queue.append(neighbor) | ||
|
|
||
| if len(result) < len(indegree): | ||
| return "" | ||
|
|
||
| return "".join(result) |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) - constant extra space
class Solution:
def canAttendMeetings(self, intervals) -> 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
📊 시간/공간 복잡도 분석
피드백: 회의 간의 중복 여부를 선형 스캔으로 확인하기 위해 시작 시간을 먼저 오름차순으로 정렬한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # N is the number of intervals. | ||
| # TC: O(N log N) - sorting the intervals by start time | ||
| # SC: O(1) - constant extra space | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def canAttendMeetings(self, intervals) -> 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 |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석non-overlapping-intervals/dolphinflow86.py# N is the number of intervals.
# TC: O(N log N) - sorts intervals by end time and performs a single pass
# SC: O(N) - space required for sorting
class Solution:
def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int:
intervals.sort(key=lambda x: x[1])
remove_count = 0
prev_end = float("-inf")
for start, end in intervals:
if start < prev_end:
remove_count += 1
else:
prev_end = end
return remove_count
📊 시간/공간 복잡도 분석
피드백: 종료점을 기준으로 정렬하고 한 번 순회하여 겹치는 구간의 수를 센다. 추가 공간은 상수로 충분하다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # N is the number of intervals. | ||
| # TC: O(N log N) - sorts intervals by end time and performs a single pass | ||
| # SC: O(N) - space required for sorting | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int: | ||
| intervals.sort(key=lambda x: x[1]) | ||
|
|
||
| remove_count = 0 | ||
| prev_end = float("-inf") | ||
|
|
||
| for start, end in intervals: | ||
| if start < prev_end: | ||
| remove_count += 1 | ||
| else: | ||
| prev_end = end | ||
|
|
||
| return remove_count |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석remove-nth-node-from-end-of-list/dolphinflow86.py# N is the number of nodes in the linked list.
# TC: O(N) - single pass using two pointers with an (n + 1) gap
# SC: O(1) - modifies links in place using constant extra variables
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head, n: int):
dummy = ListNode(0, head)
fast = dummy
slow = dummy
for _ in range(n + 1):
fast = fast.next
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
📊 시간/공간 복잡도 분석
피드백: 더블 포인터를 사용해 한 번의 순회로 제거 대상 위치를 찾는다. 추가 공간은 상수. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # N is the number of nodes in the linked list. | ||
| # TC: O(N) - single pass using two pointers with an (n + 1) gap | ||
| # SC: O(1) - modifies links in place using constant extra variables | ||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def removeNthFromEnd(self, head, n: int): | ||
| dummy = ListNode(0, head) | ||
| fast = dummy | ||
| slow = dummy | ||
|
|
||
| for _ in range(n + 1): | ||
| fast = fast.next | ||
|
|
||
| while fast: | ||
| fast = fast.next | ||
| slow = slow.next | ||
|
|
||
| slow.next = slow.next.next | ||
|
|
||
| return dummy.next |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석same-tree/dolphinflow86.py# N is the minimum number of nodes between trees p and q, and H is tree height.
# TC: O(N) - visits each node at most once comparing values
# SC: O(H) - recursion call stack proportional to tree height (O(N) in worst case)
# 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, q) -> bool:
if not p and not q:
return True
if not p or not q or p.val != q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
📊 시간/공간 복잡도 분석
피드백: 깊이우선 탐색으로 모든 노드를 방문하며 비교한다. 최악의 공간은 트리의 높이에 비례한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # N is the minimum number of nodes between trees p and q, and H is tree height. | ||
| # TC: O(N) - visits each node at most once comparing values | ||
| # SC: O(H) - recursion call stack proportional to tree height (O(N) in worst case) | ||
|
|
||
| # 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, q) -> bool: | ||
| if not p and not q: | ||
| return True | ||
| if not p or not q or p.val != q.val: | ||
| return False | ||
|
|
||
| return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
alien-dictionary/dolphinflow86.py
📊 시간/공간 복잡도 분석
피드백: 그래프를 구성하고 위상 정렬(큐 기반 BFS)을 수행하여 결과 문자열을 얻는다. 모든 문자에 대해 간선과 진입 차수를 초기화한 뒤, 0인 노드부터 처리한다.
개선 제안: 현재 구현이 적절해 보입니다.