-
-
Notifications
You must be signed in to change notification settings - Fork 305
[HYUNAHKO] WEEK 10 solutions #2277
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,41 @@ | ||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
| # 1. 그래프 만들기 (인접 리스트) | ||
| # graph[A] = [B, C] : A를 듣기 위해 B, C가 필요함 (혹은 방향에 따라 반대) | ||
| graph = collections.defaultdict(list) | ||
| for course, pre in prerequisites: | ||
| graph[course].append(pre) | ||
|
|
||
| # 2. 방문 상태 기록 (0, 1, 2) | ||
| # 0: 아직 안 가봄 (White) | ||
| # 1: 지금 탐색 중인 경로 (Grey) -> 여기서 또 만나면 뱅글뱅글 도는 것(사이클)! | ||
| # 2: 이미 검증 끝남 (Black) -> 안전함 | ||
| visit = [0] * numCourses | ||
|
|
||
| def dfs(course): | ||
| # 탐색 중인 노드를 다시 만남 == 사이클 발생! | ||
| if visit[course] == 1: | ||
| return False | ||
|
|
||
| # 이미 검증 끝난 노드 == 문제 없음 Pass | ||
| if visit[course] == 2: | ||
| return True | ||
|
|
||
| # 현재 노드를 '탐색 중(1)'으로 표시 | ||
| visit[course] = 1 | ||
|
|
||
| # 선수 과목들 쭉 파고들기 | ||
| for pre in graph[course]: | ||
| if not dfs(pre): # 재귀 호출 결과가 False(사이클 발견)라면 | ||
| return False # 즉시 False 리턴 | ||
|
|
||
| # 더 이상 갈 곳 없음. '탐색 완료(2)'로 표시 | ||
| visit[course] = 2 | ||
| return True | ||
|
|
||
| # 3. 모든 과목에 대해 확인 (그래프가 여러 덩어리일 수 있으므로) | ||
| for i in range(numCourses): | ||
| if not dfs(i): | ||
| return False | ||
|
|
||
| return True |
This file contains hidden or 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,17 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
| if not root: | ||
| return None | ||
|
|
||
| root.left, root.right = root.right, root.left | ||
|
|
||
| self.invertTree(root.left) | ||
| self.invertTree(root.right) | ||
|
|
||
| return root |
This file contains hidden or 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,15 @@ | ||
| class Solution: | ||
| def canJump(self, nums: List[int]) -> bool: | ||
| max_reachable = 0 | ||
| last_index = len(nums) - 1 | ||
|
|
||
| for i, jump_len in enumerate(nums): | ||
| if i > max_reachable: | ||
| return False | ||
|
|
||
| max_reachable = max(max_reachable, i + jump_len) | ||
|
|
||
| if max_reachable >= last_index: | ||
| return True | ||
|
|
||
| return True | ||
This file contains hidden or 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,36 @@ | ||
| # 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: | ||
| visited = set() | ||
|
|
||
| current = head | ||
| while current: | ||
| if current in visited: | ||
| return True | ||
|
|
||
| visited.add(current) | ||
| current = current.next | ||
|
|
||
| return False | ||
|
|
||
| # w/o set() | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| if not head or not head.next: | ||
| return False | ||
|
|
||
| slow = head | ||
| fast = head.next | ||
|
|
||
| while slow != fast: | ||
| if not fast or not fast.next: | ||
| return False | ||
| slow = slow.next | ||
| fast = fast.next.next | ||
|
|
||
| return True |
This file contains hidden or 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,17 @@ | ||
| class Solution: | ||
| def longestCommonSubsequence(self, text1: str, text2: str) -> int: | ||
| # Dynamic programming | ||
| n = len(text1) | ||
| m = len(text2) | ||
|
|
||
| # text1 - col, text2 - row | ||
| dp= [[0] * (n+1) for _ in range(m+1)] | ||
|
|
||
| for i in range(1, m+1): | ||
| for j in range(1, n+1): | ||
| if text2[i-1] == text1[j-1]: | ||
| dp[i][j] = dp[i-1][j-1] + 1 | ||
| else: | ||
| dp[i][j] = max(dp[i-1][j], dp[i][j-1]) | ||
|
|
||
| return dp[m][n] |
This file contains hidden or 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,26 @@ | ||
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| left_index = 0 | ||
| right_index = len(nums) -1 | ||
|
|
||
| while left_index <= right_index: | ||
| mid_index = (left_index + right_index) // 2 | ||
|
|
||
| if nums[mid_index] == target: | ||
| return mid_index | ||
|
|
||
| # 왼쪽 절반 정렬 확인 | ||
| if (nums[left_index] <= nums[mid_index]): | ||
| if (nums[left_index] <= target < nums[mid_index]): | ||
| right_index = mid_index -1 | ||
| else: | ||
| left_index = mid_index + 1 | ||
|
|
||
| # 오른쪽 절반 정렬 확인 | ||
| else: | ||
| if (nums[mid_index] < target <= nums[right_index]): | ||
| left_index = mid_index + 1 | ||
| else: | ||
| right_index = mid_index -1 | ||
|
|
||
| return -1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 dp 를 사용하기 위해 @cache를 사용했는데, greedy로 이렇게 풀 수 있군요. 공부해 갑니다!