[dolphinflow86] WEEK 12 Solutions - #2857
Conversation
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
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- ํจํด: Greedy, Sort / Order Statistics
- ์ค๋ช : ๊ฐ๊ฒฉ์ ๋๋๋ ์์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ๋ค, ๊ฐ๋ฅํ ํ ๋ง์ด ๋จ๊ธฐ๋ ค๋ ์ ํ์ ํ๋ฉฐ ์ถฉ๋์ ์ต์ํํ๋ ๊ทธ๋ฆฌ๋ ํจํด์ ์ ํ์ ์ธ ์์ด๋ค. ๋์ ๊ธฐ์ค ์ ๋ ฌ๊ณผ ๋จ์ผ ์ํ๋ก ์ค๋ณต ์ ๊ฑฐ๋ฅผ ์ํํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n log n) |
| Space | O(1) |
ํผ๋๋ฐฑ: ์ข ๋ฃ์ ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ํ ๋ฒ ์ํํ์ฌ ๊ฒน์น๋ ๊ตฌ๊ฐ์ ์๋ฅผ ์ผ๋ค. ์ถ๊ฐ ๊ณต๊ฐ์ ์์๋ก ์ถฉ๋ถํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
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- ํจํด: Two Pointers, Linked List
- ์ค๋ช : ๋ ํฌ์ธํฐ(๋ ์ค์์น ํฌ์ธํฐ)๋ก ๋๋ฆฌ ์ฌ์ฉ๋๋ ํจํด์ด๋ฉฐ, ํ๋๋ฅผ n+1 ๋งํผ ์์ ๋๊ณ , ๊ฐ์ด ์์ง์ฌ ์ ๊ฑฐํ ๋ ธ๋๋ฅผ ์ฐพ๋ ๋ฐฉ์์ด๋ค. ์ฐ๊ฒฐ๋ฆฌ์คํธ์์ ๋์์ k๋ฒ์งธ๋ฅผ ์ ๊ฑฐํ๋ๅ ธๅ ๋ฌธ์ ๋ก O(N) ๋จ์ผ ํจ์ค๋ก ํด๊ฒฐํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(1) |
ํผ๋๋ฐฑ: ๋๋ธ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํด ํ ๋ฒ์ ์ํ๋ก ์ ๊ฑฐ ๋์ ์์น๋ฅผ ์ฐพ๋๋ค. ์ถ๊ฐ ๊ณต๊ฐ์ ์์.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
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)- ํจํด: Depth-First Search, Binary Search
- ์ค๋ช : ๋ ์ด์งํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋๋ฅผ ์ฌ๊ท๋ก ๋ฐฉ๋ฌธํ๋ฉฐ ๋์ผํ์ง ๋น๊ตํ๋ DFS ํจํด์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค. ํธ๋ฆฌ์ ์ข์ฐ ์์์ ๋ํด ๊ฐ์์ง ์ฌ๊ท์ ์ผ๋ก ํ์ธํฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(h) |
ํผ๋๋ฐฑ: ๊น์ด์ฐ์ ํ์์ผ๋ก ๋ชจ๋ ๋ ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฉฐ ๋น๊ตํ๋ค. ์ต์ ์ ๊ณต๊ฐ์ ํธ๋ฆฌ์ ๋์ด์ ๋น๋กํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๐ dolphinflow86 ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
parkhojeong
left a comment
There was a problem hiding this comment.
์ด๋ฒ ์ฃผ๋ ์๊ณ ํ์ จ์ต๋๋ค!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
alien-dictionary/dolphinflow86.py
# 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)- ํจํด: Topological Sort, Hash Map / Hash Set, Breadth-First Search
- ์ค๋ช : ์ฃผ์ด์ง ์ฝ๋๋ ์ํ๋ฒณ ๊ฐ ์ ํ๊ด๊ณ๋ฅผ ๊ทธ๋ํ๋ก ํํํ๊ณ ์์ ์ ๋ ฌ์ ํตํด ์์๋ฅผ ๊ตฌํ๋ค. ๋ํ ์ธ์ ๋ฆฌ์คํธ์ indegree๋ฅผ ์ด์ฉํด ๊ฐ์ ๊ณผ ๋ ธ๋๋ฅผ ๊ด๋ฆฌํ๋ฏ๋ก ํด์ ๋งต/์ธํธ๋ฅผ ์ฌ์ฉํ๋ฉฐ BFS ์คํ์ผ์ ํ๋ฅผ ์ด์ฉํ ํ์์ผ๋ก ์์์ ๋ ฌ์ ๊ตฌํํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(N + E) |
| Space | O(U + E) |
ํผ๋๋ฐฑ: ๊ทธ๋ํ๋ฅผ ๊ตฌ์ฑํ๊ณ ์์ ์ ๋ ฌ(ํ ๊ธฐ๋ฐ BFS)์ ์ํํ์ฌ ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ์ป๋๋ค. ๋ชจ๋ ๋ฌธ์์ ๋ํด ๊ฐ์ ๊ณผ ์ง์ ์ฐจ์๋ฅผ ์ด๊ธฐํํ ๋ค, 0์ธ ๋ ธ๋๋ถํฐ ์ฒ๋ฆฌํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
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- ํจํด: Greedy, Binary Search, Two Pointers
- ์ค๋ช : ํ์์ค ๋ฐฐ์ ๋ฌธ์ ๋ ์์ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ๋ค ์ธ์ ๊ฐ๊ฒฉ์ ๋น๊ตํ์ฌ ๊ฒน์นจ์ ํ์ธํ๋ค. ๊ฐ๋จํ ํ๋์ ์กฐ๊ฑด์ ์ฒดํฌํ๋ ๋ฐฉ์์ผ๋ก ์ต์ ์ ํ์ ์์ด๋ ํด๊ฒฐ๋๋ Greedy๋ก ๋ถ๋ฅ๋๋ฉฐ, ์์์๊ฐ ์ ๋ ฌ์ ํ ๋ฐฉํฅ์ผ๋ก์ ์ ํ ๊ฒ์ฌ์ ํด๋นํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(N log N) |
| Space | O(1) |
ํผ๋๋ฐฑ: ํ์ ๊ฐ์ ์ค๋ณต ์ฌ๋ถ๋ฅผ ์ ํ ์ค์บ์ผ๋ก ํ์ธํ๊ธฐ ์ํด ์์ ์๊ฐ์ ๋จผ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!