[sangbeenmoon] WEEK 02 Solutions#2704
Conversation
๐ sangbeenmoon ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
| def isValid(mm, MM, cur: TreeNode) -> bool: | ||
|
|
||
|
|
||
| if cur == None: | ||
| return True | ||
|
|
||
| if mm != None and cur.val <= mm: | ||
| return False | ||
|
|
||
| if MM != None and cur.val >= MM: | ||
| return False | ||
|
|
||
| return isValid(mm, cur.val, cur.left) and isValid(cur.val, MM, cur.right) | ||
|
|
||
| return isValid(None, None ,root) |
There was a problem hiding this comment.
None์ is ๋ก ๋น๊ตํ๋ ๊ฒ์ผ๋ก ๊ถ์ฅ๋์ด์ ์ฐธ๊ณ ํ์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์!
Comparisons to singletons like None should always be done with is or is not, never the equality operators.
https://peps.python.org/pep-0008/#programming-recommendations
๊ทธ๋ฆฌ๊ณ ๋ณ์๋ช ์ mm, MM์ด ์ด๋ค ๊ฐ์ธ์ง ์๊ธฐ๊ฐ ์ด๋ ค์์ ์๋ฏธ๋ฅผ ๋ด์ ๋ณด์๋ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค :)
| def isValid(mm, MM, cur: TreeNode) -> bool: | |
| if cur == None: | |
| return True | |
| if mm != None and cur.val <= mm: | |
| return False | |
| if MM != None and cur.val >= MM: | |
| return False | |
| return isValid(mm, cur.val, cur.left) and isValid(cur.val, MM, cur.right) | |
| return isValid(None, None ,root) | |
| def isValid(max_val, min_val, cur: TreeNode) -> bool: | |
| if cur is None: | |
| return True | |
| if max_val is not None and cur.val <= max_val: | |
| return False | |
| if min_val is not None and cur.val >= min_val: | |
| return False | |
| return isValid(max_val, cur.val, cur.left) and isValid(cur.val, min_val, cur.right) | |
| return isValid(None, None, root) |
There was a problem hiding this comment.
์ค ์ด๋ฐ ๊ถ์ฅ์ฌํญ์ด ์๋์ง๋ ๋ชฐ๋๋ค์ ใ ใ ๊ณต์๋ฌธ์๊น์ง ์ฐพ์์ ์ ์ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค ๐
| while left < right: | ||
| if nums[left] + nums[right] == target: | ||
| candidate = (target * -1, nums[left], nums[right]) | ||
| if candidate not in answer_map: | ||
| answer_map[candidate] = True | ||
| answer.append([target * -1, nums[left], nums[right]]) |
There was a problem hiding this comment.
ํด์๋งต์ ์ฌ์ฉํด์ candidate๋ฅผ ๋ด์์ ์ค๋ณต์ ์ฒดํฌํ์
จ๊ตฐ์!
์ด๋ฏธ ์ ๋ ฌ์ด ๋ ์ํ๋ผ์ nums[left] == nums[left+1] ์ธ ์ผ์ด์ค๋ฅผ ์คํตํด์ฃผ๋ฉด(right๋ ๋์ผ) candidate๋ฅผ ์ฌ์ฉํ์ง ์๊ณ not in ๋ก์ง์ ์ํ๊ธฐ ๋๋ฌธ์ ๋ ์ต์ ํ๊ฐ ๊ฐ๋ฅํ ๊ฑฐ ๊ฐ์์!
There was a problem hiding this comment.
์ข ๋ ๊น๋ํ๊ฒ ์ค๋ณต ์ฒดํฌํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๋ค์. ์ง์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค
| if candidate not in answer_map: | ||
| answer_map[candidate] = True | ||
| answer.append([target * -1, nums[left], nums[right]]) | ||
| left += 1 |
There was a problem hiding this comment.
์ฑ๊ณตํ ๊ฒฝ์ฐ right -= 1 ๋ ํจ๊ป ํด์ฃผ๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์!
| for i in range(len(nums)): | ||
| target = -1 * nums[i] | ||
|
|
||
| left = i + 1 | ||
| right = len(nums) - 1 |
There was a problem hiding this comment.
์ ๋ ฌ์ด ๋ ์ํ๋ผ nums[i] ์ nums[i - 1] ์ด ๊ฐ์ ๊ฒฝ์ฐ๋ ์คํตํด์ฃผ๊ณ nums[i] > 0 ์ด ๋ ์ํ์์๋ break ํด์ฃผ๋ฉด ์ต์ ํ๊ฐ ๊ฐ๋ฅํ ๊ฑฐ ๊ฐ์ต๋๋ค :)
ex. [ -2, -1, -1, -1, 0, 1, 2, 3] , -1์ด ํ๋ฒ์ธ ๊ฒฝ์ฐ๋ง ์คํํ๊ณ ์ดํ๋ ์คํต, nums[i]๊ฐ 0๋ณด๋ค ์ปค์ง๋ฉด ์์ + ์์ + ์์ ์ด๊ธฐ ๋๋ฌธ์ ์ข
๋ฃ
alphaorderly
left a comment
There was a problem hiding this comment.
๊น๋ํ๊ฒ ์ ํด๊ฒฐํ์ ๊ฒ ๊ฐ์ต๋๋ค! ๊ณ ์ํ์ จ์ด์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
- ํจํด: Two Pointers, Hash Map / Hash Set
- ์ค๋ช : ์ผํฉ(3Sum) ๋ฌธ์ ์์ ์ ๋ ฌ ํ ์ ๋ ํฌ์ธํฐ๋ฅผ ์ด๋ํ๋ ๋ฐฉ์์ผ๋ก ํ์ํ๋ฏ๋ก Two Pointers ํจํด์ด ์ ์ฉ๋ฉ๋๋ค. ๋ํ ์ค๋ณต ์ ๊ฑฐ๋ฅผ ์ํด Hash Map/Hash Set๋ฅผ ํ์ฉํด ๊ฒฐ๊ณผ ์ค๋ณต์ ๋ฐฉ์งํ๋ ๋ถ๋ถ์ด ์์ต๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(n^2) | O(n^2) | โ |
| Space | O(n) | O(1) | โ |
ํผ๋๋ฐฑ: ์ ๋ ฌ ํ ์ธ๋ถ ๋ฃจํ์ ๋ด๋ถ ํฌ ํฌ์ธํฐ๋ก ๋ณต์ก๋๋ฅผ ๋ฌ์ฑํ๋ค. ๋ค๋ง ์ค๋ณต ์ ๊ฑฐ๋ฅผ visited๋ก ์ถ๊ฐํ๋๋ฐ, ์ ์ฒด์ ์ผ๋ก ๋ถํ์ํ ํด์๋งต ์ฌ์ฉ ๊ฐ๋ฅ์ฑ์ด ์๋ค. ๋ ๋ฒ์งธ ๊ตฌํ์ฒ๋ผ ์ค๋ณต ์ ๊ฑฐ๋ฅผ ๋ ๊ฐ๊ฒฐํ๊ฒ ์ฒ๋ฆฌํ๋ ํธ์ด ์ข๋ค.
๊ฐ์ ์ ์: ๊ณ ๋ คํด๋ณผ ๋งํ ๋์: ์ค๋ณต ์ ๊ฑฐ๋ฅผ ๋ฆฌ์คํธ ์คํต ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๊ณ ํด์๋งต ์์ด๋ ์ค๋ณต ์๋ ๊ฒฐ๊ณผ๋ฅผ ์ป๋๋ก ๊ตฌํ์ ๋จ์ํ
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
- ํจํด: Depth-First Search, Binary Search, Hash Map / Hash Set
- ์ค๋ช : ์ฒซ ๋ฒ์งธ ์ฝ๋๋ ์ฌ๊ท๋ก BST์ ๊ฐ ๋ ธ๋๋ฅผ ํ์ํ๋ฉฐ ์์ ๋ ธ๋๊ฐ ํน์ ๋ฒ์ ์์ ์๋์ง ํ์ธํ๋ค. ๋ ๊ฐ์ง ๊ตฌํ ๋ชจ๋ ํธ๋ฆฌ ๊ตฌ์กฐ์ ๊ฐ ๋ ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ DFS ํจํด์ด๋ฉฐ, ์์ ๋ ธ๋์ ๊ฐ ๋ฒ์๋ฅผ ์ ํํด BST ํน์ฑ์ ๊ฒ์ฌํ๋ ๋ฐฉ์์ด๋ค. ๋ ๋ฒ์งธ ๊ตฌํ์ min/max ๋ฒ์ ์ถ์ ์ ํตํ Divide and Conquer์ DFS๋ก BST ์ฌ๋ถ๋ฅผ ํ๋จํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n) |
| Space | O(h) |
ํผ๋๋ฐฑ: ๋ ๊ฐ์ง ๊ตฌํ์ด ์์ฌ ์๋๋ฐ, ์ฒซ ๋ฒ์งธ ๊ตฌํ์ ์ ์ญ ์ํ๋ฅผ ์ด์ฉํด ์ฌ๊ท์ ์ผ๋ก ๋ฒ์๋ฅผ ๊ฒ์ฌํ๋ค. ๋ ๋ฒ์งธ๋ ์ฌ๊ท์ ์ผ๋ก ๋ฒ์๋ฅผ ๋๊ฒจ์ฃผ๋ ์ผ๋ฐ์ ์ธ ํจํด์ผ๋ก ์์ ์ ์ด๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!