-
-
Notifications
You must be signed in to change notification settings - Fork 305
[radiantchoi] WEEK 10 Solutions #2270
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,25 @@ | ||
| from collections import deque, defaultdict | ||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
| posts = defaultdict(list) | ||
| indegree = [0] * numCourses | ||
|
|
||
| for prerequisite in prerequisites: | ||
| posts[prerequisite[1]].append(prerequisite[0]) | ||
| indegree[prerequisite[0]] += 1 | ||
|
|
||
| completed = 0 | ||
| q = deque(list(filter(lambda x: indegree[x] == 0, range(numCourses)))) | ||
|
|
||
| while q: | ||
| current = q.popleft() | ||
| completed += 1 | ||
|
|
||
| for post in posts[current]: | ||
| indegree[post] -= 1 | ||
|
|
||
| if indegree[post] == 0: | ||
| q.append(post) | ||
|
|
||
| return completed == numCourses |
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,28 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * class TreeNode { | ||
| * val: number | ||
| * left: TreeNode | null | ||
| * right: TreeNode | null | ||
| * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| * } | ||
| */ | ||
|
|
||
| function invertTree(root: TreeNode | null): TreeNode | null { | ||
| if (root === null) { | ||
| return null; | ||
| } | ||
|
|
||
| const temp = root.left; | ||
| root.left = root.right; | ||
| root.right = temp; | ||
|
|
||
| invertTree(root.left); | ||
| 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,11 @@ | ||
| class Solution: | ||
| def canJump(self, nums: List[int]) -> bool: | ||
| reachable = [False] * len(nums) | ||
| destination = len(nums) - 1 | ||
|
|
||
| for index, strength in list(enumerate(nums))[::-1]: | ||
| if index + strength >= destination: | ||
| reachable[index] = True | ||
| destination = index | ||
|
|
||
| return reachable[0] | ||
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 { | ||
| func search(_ nums: [Int], _ target: Int) -> Int { | ||
| var left = 0 | ||
| var right = nums.count - 1 | ||
|
|
||
| while left <= right { | ||
| let mid = (left + right) / 2 | ||
|
|
||
| // 일단, 찾았으면 바로 반환 | ||
| if nums[mid] == target { | ||
| return mid | ||
| } | ||
|
|
||
| // 이 문제의 배열은 정렬되지 않은 것이 아니고, 정렬된 배열 두 개를 붙여 놨다고 생각하면 된다. | ||
| // 유효한 인덱스임이 보장된 left와 현재 인덱스인 mid를 비교한다. | ||
| if nums[left] <= nums[mid] { | ||
| // 이로써 left와 mid 사이가 이 "정렬된 부분"임을 체크한다. | ||
| // 정렬이 되어 있다면, 찾고자 하는 값이 이 "정렬된 범위" 내에 있는지 확인한다. | ||
| // left에서 >=를 이용한 이유는, left 자체가 정답 인덱스일 수도 있기 때문. | ||
| if target >= nums[left] && target < nums[mid] { | ||
| // 만약 그렇다면 통상적인 이진 탐색의 논리대로 가면 된다. | ||
| right = mid - 1 | ||
| } else { | ||
| // 만약 그렇지 않다면 원래 갔어야 할 방향의 반대 방향으로 가면 된다. | ||
| left = mid + 1 | ||
| } | ||
| } else { | ||
| // left와 mid 사이가 정렬이 틀어져 있다면, mid와 right 사이는 정렬되어 있는 것이다. | ||
| // 위와 동일하게 범위 확인을 하되, 방향을 반대로 가져가면 된다. | ||
| if target <= nums[right] && target > nums[mid] { | ||
| left = mid + 1 | ||
| } else { | ||
| right = mid - 1 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // early return하지 못했다면, 배열 내에 찾고자 하는 수가 없는 것이다. | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
안녕하세요 해당 풀이도 잘 풀어주셨는데,
reachable 리스트를 따로 만들지 않고 마지막에 return destination == 0으로 변경할 수 있을거 같아요
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.
오.. 그런 방법도 좋겠네요! 이 문제같은 경우는 풀 때 고생을 많이 했어서, "목적지부터 시작하여 시작점에서 이를 수 있는가" 라는 도식을 구현하는 데 집중했습니다. 사람의 인식과 효율적인 코드란 다를 수 있음을 새삼 느끼게 되네요.