Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chaedie] Week 10 #1023

Merged
merged 5 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions course-schedule/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
(해설을 보면서 따라 풀었습니다.)

Solution: graph 에서 circular 가 생기면 false, 아니면 true

n: numCourses
p: number of preRequisites
Time: O(n + p)
Space: O(n + p)
"""


class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
graph = {i: [] for i in range(numCourses)}
for crs, pre in prerequisites:
graph[crs].append(pre)

traversing = set()
finished = set()

def dfs(crs):
if crs in traversing:
return False
if crs in finished:
return True

traversing.add(crs)
for pre in graph[crs]:
if not dfs(pre):
return False
traversing.remove(crs)
finished.add(crs)
return True

for crs in graph:
if not dfs(crs):
return False
return True
16 changes: 16 additions & 0 deletions invert-binary-tree/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Time: O(n)
Space: O(n)
"""


class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None

temp = root.right
root.right = self.invertTree(root.left)
root.left = self.invertTree(temp)

return root
37 changes: 37 additions & 0 deletions jump-game/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution:
"""
Solution 1:
뒤에서 부터 푸는 방법이 있을거라 생각했습니다.
풀이는 성공했지만 성능은 느린 5% 라는 느린 성능을 보입니다.

Time: O(n * 10^5) - 10^5 는 nums 원소의 최대 값
Space: O(n)
"""

def canJump(self, nums: List[int]) -> bool:
dp = [False for i in range(len(nums))]

dp[len(nums) - 1] = True

for i in range(len(nums) - 1 - 1, -1, -1):
for j in range(1, nums[i] + 1):
if i + j < len(nums) and dp[i + j]:
dp[i] = True
break

return dp[0]

"""
Solution 2:
Greedy - 솔루션을 참고했습니다.

Time: O(n)
Space: O(1)
"""

def canJump(self, nums: List[int]) -> bool:
reach = 0
for idx in range(len(nums)):
if idx <= reach:
reach = max(reach, idx + nums[idx])
return reach >= len(nums) - 1
27 changes: 27 additions & 0 deletions merge-k-sorted-lists/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Solution:
1) 모든 linked list 의 value 를 arr에 담는다.
2) arr 를 정렬한다.
3) arr 로 linked list 를 만든다.

Time: O(n log(n)) = O(n) arr 만들기 + O(n log(n)) 정렬하기 + O(n) linked list 만들기
Space: O(n)
"""


class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
arr = []
for linked_list in lists:
while linked_list:
arr.append(linked_list.val)
linked_list = linked_list.next

dummy = ListNode()
node = dummy

arr.sort()
for num in arr:
node.next = ListNode(num)
node = node.next
return dummy.next
77 changes: 77 additions & 0 deletions search-in-rotated-sorted-array/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
1) binary search 로 pivot 찾아 2번 binary search 하기
Time: O(log(n))
Space: O(1)
"""


class Solution:
def search(self, nums: List[int], target: int) -> int:

def find_pivot():
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if mid > 0 and nums[mid - 1] >= nums[mid]:
return mid
if nums[0] <= nums[mid]:
low = mid + 1
else:
high = mid - 1
return 0

def binary_search(low, high):
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

pivot = find_pivot()

idx = binary_search(0, pivot - 1)
return idx if idx > -1 else binary_search(pivot, len(nums) - 1)


"""
2) binary search
left sorted case
4 5 6 7 0 1 2 3
m
left target case
right target case

right sorted case
6 7 0 1 2 3 4 5
m
left target case
right target case
Time: O(log(n))
Space: O(1)
"""


class Solution:
def search(self, nums: List[int], target: int) -> int:

low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if target == nums[mid]:
return mid

if nums[low] <= nums[mid]:
if nums[low] <= target <= nums[mid]:
high = mid - 1
else:
low = mid + 1
else:
if nums[mid] <= target <= nums[high]:
low = mid + 1
else:
high = mid - 1
return -1