[yuseok89] WEEK 09 Solutions - #2824
Conversation
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
linked-list-cycle/yuseok89.py
# TC: O(N)
# SC: O(1)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return False
slow, fast = head, head.next
while slow != fast:
if not fast or not fast.next:
return False
slow, fast = slow.next, fast.next.next
return True
- ํจํด: Two Pointers, Fast & Slow Pointers
- ์ค๋ช : ํด๋น ์ฝ๋๋ ๋๋ฆฐ ํฌ์ธํฐ์ ๋น ๋ฅธ ํฌ์ธํฐ๋ฅผ ์ด์ฉํด ์ํ ์ฌ๋ถ๋ฅผ ํ๋ณํ๋ ์ ํ์ ์ธ Fast & Slow Pointers ํจํด์ด๋ค. ๋ ํฌ์ธํฐ์ ์๋ ์ฐจ์ด๋ฅผ ์ด์ฉํด ์ฌ์ดํด ์กด์ฌ ์ฌ๋ถ๋ฅผ O(N) ์๊ฐ, O(1) ๊ณต๊ฐ์ผ๋ก ํ๋จํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(1) |
ํผ๋๋ฐฑ: ๋น ๋ฅธ ํฌ์ธํฐ์ ๋๋ฆฐ ํฌ์ธํฐ๋ฅผ ์ด์ฉํด ์ํํ๋ฏ๋ก ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์์ด ์ฌ์ดํด ์ฌ๋ถ๋ฅผ ํ๋จํ ์ ์๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๐ yuseok89 ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
There was a problem hiding this comment.
์กฐ๊ฑด๋ฌธ ๋ถ๋ถ์ ๊ฐ์์ง ๋น๊ต๋ฅผ ํ๋๊ฒ๋ ๊ฝค ๊ด์ฐฎ์ ์์ด๋์ด์๋ค์!
๋ค๋ง ์์ฃผ ๊ฐ์ธ์ ์ธ ์๊ฒฌ์ธ๋ฐ, slow์ fast๋ฅผ ๋น๊ตํ๋๊ฒ์ ์ฐธ์กฐ๋ฅผ ๋น๊ตํ๋๊ฑฐ๋ผ is๋ฅผ ์ฐ๋๊ฒ ๋ ๋ซ์ง ์์๊น? ํ๋ ์๊ฐ์ด ์์์ด์
์ข์ ์ฝ๋ ๋ณด๊ณ ๊ฐ๋๋ค!
There was a problem hiding this comment.
๋ ๋ช
ํํด์ง๊ฒ ๋ค์.
๋ฐ์ํด๋ณด๊ฒ ์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค !
| if not head or not head.next: | ||
| return False |
There was a problem hiding this comment.
๋
ธ๋๊ฐ ํ๋๋ฟ์ด๊ณ ์ํ์ด ์๋ ์
๋ ฅ์ด๋ผ๋ฉด fast๊ฐ None์ผ๋ก ์์ํ๋ฏ๋ก, ๋ฐ๋ณต๋ฌธ ์์ if not fast ๊ฐ ๊ณง๋ฐ๋ก False๋ฅผ ๋ฐํํด ์คํ
๋ฐ์. ๊ทธ๋ ๋ค๋ฉด ์ฌ๊ธฐ์ not head.next๊น์ง ํ์ธํ๋ ๊ฒ์ ์ด๋ค ๊ฒฝ์ฐ๋ฅผ ๋์ํ๊ธฐ ์ํ ๋ชฉ์ ์ธ๊ฐ์?
There was a problem hiding this comment.
์กฐ๊ธ ๊ณผํ๊ฒ ์๊ฐํ ๊ฒ ๊ฐ์ต๋๋ค.
๋ง์ ํ์ ๋๋ก, not head ๋ง์ผ๋ก ์ถฉ๋ถํฉ๋๋ค.
๊ฐ์ฌํฉ๋๋ค !
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
linked-list-cycle/yuseok89.py
# TC: O(N)
# SC: O(1)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head:
return False
slow, fast = head, head.next
while slow is not fast:
if not fast or not fast.next:
return False
slow, fast = slow.next, fast.next.next
return True
- ํจํด: Fast & Slow Pointers
- ์ค๋ช : ํด๋น ์ฝ๋๋ ํ ๋ ธ๋๋ ํ ์นธ, ๋ค๋ฅธ ๋ ธ๋๋ ๋ ์นธ์ฉ ์งํํ๋ ๋ ํฌ์ธํฐ๋ฅผ ์ด์ฉํด ์ํ ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ฏ๋ก Fast & Slow Pointers ํจํด์ ์ํฉ๋๋ค. ์ฌ์ดํด ์กด์ฌ ์ฌ๋ถ๋ฅผ O(N) ์๊ฐ๊ณผ O(1) ๊ณต๊ฐ์ผ๋ก ํ๋ณํฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(1) |
ํผ๋๋ฐฑ: ํด๋น ์๊ณ ๋ฆฌ์ฆ์ ํ ๋ฒ์ ์ํ๋ก ์ฌ์ดํด ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ์ผ๋ฐ์ ์ธ ํด์ ์์ด๋ ๊ฐ๋ฅํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
maximum-product-subarray/yuseok89.py
# TC: O(N)
# SC: O(1)
class Solution:
def maxProduct(self, nums: List[int]) -> int:
ans = nums[0]
max_val, min_val = 1, 1
for num in nums:
temp = max_val * num
max_val = max(temp, min_val * num, num)
min_val = min(temp, min_val * num, num)
ans = max(ans, max_val)
return ans
- ํจํด: Dynamic Programming, Two Pointers, Greedy
- ์ค๋ช : ์ฐ์ ๋ถ๋ถ ๋ฐฐ์ด์ ๊ณฑ์ ์ต๋๊ฐ์ ์ ์งํ๋ฉฐ ํ์ฌ ๊ตฌ๊ฐ์ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ๊ฐฑ์ ํ๋ ๋ฐฉ์์ผ๋ก, ์ฐ์ ๊ณฑ์ ๋ถํธ ๋ณํ์ ๋์ํ๋ DP/์ํ ์ ์ง ํจํด์ผ๋ก ํ์ด๋๋ค. ๋ํ ํ ๋ฒ์ ์ํ๋ก ํด๊ฒฐํ๋ฏ๋ก Sliding Window์ ๋น์ทํ ํ๋ฆ์ด ์์ง๋ง ํต์ฌ์ ๋ถ๋ถ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ ๋์ ๊ฒฐ์ ์ด๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(N) | O(n) | โ |
| Space | O(1) | O(1) | โ |
ํผ๋๋ฐฑ: ์์/์์์ ๊ณฑ์ผ๋ก ์ธํด ์ต๋/์ต์๋ฅผ ํจ๊ป ๊ณ ๋ คํ๋ ํ์ค ํ์ด๋ฅผ ์ฌ์ฉํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
minimum-window-substring/yuseok89.py
# TC: O(N)
# SC: O(N)
class Solution:
def minWindow(self, s: str, t: str) -> str:
len_s = len(s)
len_t = len(t)
min_len = len_s + 1
ans_start_idx = -1
total_cnt = 0
t_cnt_dict = Counter(t)
cnt_dict = defaultdict(int)
l, r = 0, 0
while r < len_s:
r_c = s[r]
if cnt_dict[r_c] < t_cnt_dict.get(r_c, 0):
total_cnt += 1
cnt_dict[r_c] += 1
r += 1
while total_cnt == len_t:
if min_len > r - l:
min_len = r - l
ans_start_idx = l
l_c = s[l]
if cnt_dict[l_c] <= t_cnt_dict[l_c]:
total_cnt -= 1
cnt_dict[l_c] -= 1
l += 1
if ans_start_idx == -1:
return ''
return s[ans_start_idx:ans_start_idx + min_len]
- ํจํด: Sliding Window, Hash Map / Hash Set, Two Pointers
- ์ค๋ช : ๋ ํฌ์ธํฐ(l, r)๋ก ์ฐฝ์ ํ์ฅํ๊ณ ์ถ์ํ๋ฉด์ ํ์ํ ๋ฌธ์ ๊ฐ์๋ฅผ ํด์๋งต์ผ๋ก ๊ด๋ฆฌํ๋ ํ์ค Sliding Window ํจํด์ผ๋ก ํ์ดํฉ๋๋ค. ํด์ ๋งต์ ์ฌ์ฉํด ๋ฌธ์์ ๋น๋์๋ฅผ ์ถ์ ํ๊ณ ์ต์ ์ฐฝ์ ๊ฐฑ์ ํฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(N) | O( | s |
| Space | O(N) | O( | alphabet |
ํผ๋๋ฐฑ: ์ฉ๋์ ๋ง๋ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ + ์นด์ดํธ ๋งคํ์ผ๋ก ์ ํ ์๊ฐ ๋ณต์ก๋๋ฅผ ์ป๋๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
pacific-atlantic-water-flow/yuseok89.py
# TC: O(N*M)
# SC: O(N*M)
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
n = len(heights)
m = len(heights[0])
dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]]
def dfs(row, col, check):
check[row][col] = True
for dir in dirs:
new_row = row + dir[0]
new_col = col + dir[1]
if 0 <= new_row < n and 0 <= new_col < m and not check[new_row][new_col] and heights[row][col] <= heights[new_row][new_col]:
dfs(new_row, new_col, check)
pac_check = [[False for _ in range(m)] for _ in range(n)]
atl_check = [[False for _ in range(m)] for _ in range(n)]
for row in range(1, n):
dfs(row, 0, pac_check)
for col in range(m):
dfs(0, col, pac_check)
for row in range(n - 1):
dfs(row, m - 1, atl_check)
for col in range(m):
dfs(n - 1, col, atl_check)
ans = []
for row in range(n):
for col in range(m):
if pac_check[row][col] and atl_check[row][col]:
ans.append([row, col])
return ans
- ํจํด: Depth-First Search, Binary Search
- ์ค๋ช : ์ ํฅ ํ๋ฆ ๋์ ๋ ํด๋ฐ์ ๊ฒฝ๊ณ๋ถํฐ DFS๋ก ํผ์ ธ๋๊ฐ๋ฉฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์น๋ฅผ ์ฐพ๋ ๊ตฌ์กฐ๋ก, ๋ ๋ฐ๋ค์์ ๋๋ฌ ๊ฐ๋ฅํ์ง ํ์ธํ๊ธฐ ์ํด DFS๋ฅผ ์ฌ์ฉํฉ๋๋ค. ํจํด์ DFS์ ํด๋นํ๋ฉฐ, ์ด ๋ฌธ์ ์ ํต์ฌ์ ๋ ๋ฐฉํฅ์ผ๋ก์ ํ์์ ํตํ ๊ต์งํฉ ํ์ธ์ ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(N*M) | O(nm) | โ |
| Space | O(N*M) | O(nm) | โ |
ํผ๋๋ฐฑ: ๊ฐ ์ ์ ๋ํด ๋ ๋ฐฉํฅ์ผ๋ก ๋๋ฌ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ๊ธฐ๋กํ๊ณ , ์์์ ์์ ์ธ์ ํ ์ ๋ก์ ํ๋ฆ ์กฐ๊ฑด์ ์ฌ๊ท์ ์ผ๋ก ํ์ฅํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
sum-of-two-integers/yuseok89.py
# TC: O(12)
# SC: O(1)
class Solution:
def getSum(self, a: int, b: int) -> int:
MASK = 0b111111111111
a = a & MASK
b = b & MASK
while b != 0:
c = (a & b) << 1
a = a ^ b
b = c & MASK
if a > (MASK >> 1):
a = ~(a ^ MASK)
return a
- ํจํด: Bit Manipulation, Greedy
- ์ค๋ช : ๋ ์ ์์ ํฉ์ ๋นํธ ์ฐ์ฐ์ผ๋ก ๊ตฌํํ๋ ๋ฐฉ์์ผ๋ก, ๋นํธ ์ฐ์ฐ๊ณผ ๋ง์คํฌ๋ฅผ ์ด์ฉํด ํฉ์ ๊ตฌํ๋ ์ ์์ Bit Manipulation ํจํด์ ์ํฉ๋๋ค. ์ถ๊ฐ์ ์ผ๋ก ์ต์ ํ๋ ์์ ๊ณต๊ฐ ์ฌ์ฉ์ ๋ฌธ์ ํด๊ฒฐ์ ์ผ๋ฐ์ ํน์ง์ผ๋ก ๋ณด์ผ ์ ์์ต๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(12) | O(1) | โ |
| Space | O(1) | O(1) | โ |
ํผ๋๋ฐฑ: ๋นํธ ์ฐ์ฐ์ผ๋ก ๋ฌธ์์ด์ ๊ตฌํ ์์ด ๋ง์ ์ ๊ตฌํํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!