-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from mangodm-web/main
[mangodm-web] Week10 Solutions
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
""" | ||
- Idea: ๊ฐ ๊ณผ๋ชฉ์ ์ ํ ๊ณผ๋ชฉ์ ์ฌ์ดํด์ด ์กด์ฌํ๋์ง DFS๋ก ํ์ํ๋ค. | ||
ํ๋๋ผ๋ ์ฌ์ดํด์ด ์กด์ฌํ๋ค๋ฉด, ๋ชจ๋ ๊ณผ๋ชฉ์ ์๊ฐํ ์ ์๋ค๋ ์๋ฏธ๋ค. | ||
- Time Complexity: O(v + e). v์ e๋ ๊ฐ๊ฐ ๊ณผ๋ชฉ์ ์, e๋ ์ ํ ๊ด๊ณ(๊ณผ๋ชฉ => ์ ํ ๊ณผ๋ชฉ)์ ์๋ค. | ||
๋ชจ๋ ๊ณผ๋ชฉ๊ณผ ๊ทธ ๊ณผ๋ชฉ์ ์ ํ ๊ณผ๋ชฉ์ ํ์ํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๊ฐ ๋ ธ๋์ ์ฃ์ง์ ๋ํด ํ๋ฒ์ฉ ๋ฐฉ๋ฌธํด์ผ ํ๋ค. | ||
- Space Complexity: O(v + e). v์ e๋ ๊ฐ๊ฐ ๊ณผ๋ชฉ์ ์, e๋ ์ ํ ๊ด๊ณ์ ์๋ค. | ||
๊ฐ ๊ณผ๋ชฉ์ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ๊ธฐ๋กํ๊ธฐ ์ํด O(v) ๊ณต๊ฐ์ด ํ์ํ๊ณ , | ||
๊ณผ๋ชฉ ๊ฐ ์ ํ ๊ด๊ณ๋ฅผ ์ ์ฅํ๋ ์ธ์ ๋ฆฌ์คํธ๋ O(e)์ ๊ณต๊ฐ์ ์ฐจ์งํ๋ค. | ||
""" | ||
|
||
graph = {i: [] for i in range(numCourses)} | ||
|
||
for course, prerequisite in prerequisites: | ||
graph[course].append(prerequisite) | ||
|
||
visited = set() | ||
|
||
def DFS(course): | ||
if course in visited: | ||
return False | ||
if graph[course] == []: | ||
return True | ||
|
||
visited.add(course) | ||
for prerequisite in graph[course]: | ||
if not DFS(prerequisite): | ||
return False | ||
|
||
visited.remove(course) | ||
graph[course] = [] | ||
|
||
return True | ||
|
||
for course in range(numCourses): | ||
if not DFS(course): | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Optional | ||
|
||
|
||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
|
||
class Solution: | ||
""" | ||
- Idea: ์ฌ๊ท๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ๋ ธ๋์ ์ผ์ชฝ ์์๊ณผ ์ค๋ฅธ์ชฝ ์์์ ๋ฐ๊พผ๋ค. | ||
- Time Complexity: O(n). n์ ์ ์ฒด ๋ ธ๋์ ์๋ค. | ||
๋ชจ๋ ๋ ธ๋์ ๋ํด์ ๋ฐ๋ณต์ ์ผ๋ก ์ํํด์ผ ํ๊ธฐ ๋๋ฌธ์ O(n) ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค. | ||
- Space Complexity: O(n). n์ ์ ์ฒด ๋ ธ๋์ ์๋ค. | ||
์ต์ ์ ๊ฒฝ์ฐ, ๋ถ๊ท ํ ํธ๋ฆฌ์์๋ ์ฌ๊ท ํธ์ถ๋ก ์ธํด ์ต๋ O(n) ์คํ ๊ณต๊ฐ์ด ํ์ํ๋ค. | ||
""" | ||
|
||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
if root is None: | ||
return | ||
|
||
root.left, root.right = root.right, root.left | ||
|
||
self.invertTree(root.left) | ||
self.invertTree(root.right) | ||
|
||
return root |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
""" | ||
- ์์ด๋์ด: ๋ฐฐ์ด์ ์ค๊ฐ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ์ค ํ์ชฝ์ ํญ์ ์ ๋ ฌ๋์ด ์๋ค. | ||
์ด ํน์ง์ ์ฐฉ์ํ์ฌ, ์ด๋ ์ชฝ์ด ๋จผ์ ์ ๋ ฌ๋์ด ์๋์ง ํ์ธํ๊ณ , ๊ทธ ์์ | ||
์ฐพ์ผ๋ ค๋ ๊ฐ์ด ์๋์ง๋ฅผ ํ์ธํ๋ ๋ฐฉ์์ผ๋ก ํ์ ๋ฒ์๋ฅผ ์ขํ๊ฐ๋ค. | ||
- ์๊ฐ ๋ณต์ก๋: O(logn). n์ ๋ฐฐ์ด์ ๊ธธ์ด. | ||
๋ฐฐ์ด์ ์ ๋ฐ์ฉ ๋๋์ด ํ์์ ์งํํ๊ธฐ ๋๋ฌธ์ ์๊ฐ ๋ณต์ก๋๋ O(logn)์ด๋ค. | ||
- ๊ณต๊ฐ ๋ณต์ก๋: O(1). ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ์์ ๊ณต๊ฐ๋ง ํ์ํ๋ค. | ||
""" | ||
|
||
def search(self, nums: List[int], target: int) -> int: | ||
left, right = 0, len(nums) - 1 | ||
|
||
while left <= right: | ||
mid = (left + right) // 2 | ||
|
||
if nums[mid] == target: | ||
return mid | ||
|
||
if nums[left] <= nums[mid]: | ||
if nums[left] <= target < nums[mid]: | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
else: | ||
if nums[mid] < target <= nums[right]: | ||
left = mid + 1 | ||
else: | ||
right = mid - 1 | ||
|
||
return -1 |