Skip to content

Commit

Permalink
Merge pull request #543 from mangodm-web/main
Browse files Browse the repository at this point in the history
[mangodm-web] Week10 Solutions
  • Loading branch information
mangodm-web authored Oct 21, 2024
2 parents f6c5877 + 4448abc commit ef6bd30
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
43 changes: 43 additions & 0 deletions course-schedule/mangodm-web.py
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
29 changes: 29 additions & 0 deletions invert-binary-tree/mangodm-web.py
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
34 changes: 34 additions & 0 deletions search-in-rotated-sorted-array/mangodm-web.py
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

0 comments on commit ef6bd30

Please sign in to comment.