-
Notifications
You must be signed in to change notification settings - Fork 0
Course Schedule #106
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
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
207.Course-Schedule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Course Schedule #106
Changes from all commits
Commits
Show all changes
2 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,35 @@ | ||
| # 207. Course Schedule | ||
|
|
||
| ## step1 | ||
|
|
||
| DAG判定だと思う。トポロジカルソート。 | ||
|
|
||
| step1_loop.pyを10mで書いたが遅い。おそらく set の再構築に時間がかかっているのだろう。 | ||
|
|
||
| step1_backtrack.pyにすると多少速くなった。 | ||
|
|
||
| ただ空間計算量が O(V^2) | ||
|
|
||
| ## step2 | ||
|
|
||
| ### 他の人のコード | ||
|
|
||
| https://github.com/huyfififi/coding-challenges/pull/34 | ||
|
|
||
| > NOT_VISITED_YET = 0 | ||
| > VISITING = 1 | ||
| > VISITED = 2 | ||
|
|
||
| これを使って書き直すと、loopの方で速度がBeats 100%となった。 | ||
|
|
||
| > トポロジカルソートの中でも特にKahn's algorithmと呼ばれるもの | ||
|
|
||
| https://ja.wikipedia.org/wiki/%E3%83%88%E3%83%9D%E3%83%AD%E3%82%B8%E3%82%AB%E3%83%AB%E3%82%BD%E3%83%BC%E3%83%88 | ||
|
|
||
| これを実装。個人的にはqueueを使わないもののほうがわかりやすい。 | ||
|
|
||
| https://github.com/potrue/leetcode/pull/76 | ||
|
|
||
| https://github.com/thonda28/leetcode/pull/3 | ||
|
|
||
| https://github.com/thonda28/leetcode/pull/3 | ||
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,27 @@ | ||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
|
|
||
| checked: set[int] = set() | ||
|
|
||
| def no_cycle(node, seen): | ||
| if node in seen: | ||
| return False | ||
| if node in checked: | ||
| return True | ||
|
|
||
| seen.add(node) | ||
| for child in adjacent_list[node]: | ||
| if not no_cycle(child, seen): | ||
| return False | ||
| seen.remove(node) | ||
| checked.add(node) | ||
| return True | ||
|
|
||
| for root in range(numCourses): | ||
| if not no_cycle(root, set()): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 二重否定は読み手に取ってやや認知負荷が高くなると思います。 inner function を has_cycle() とし、 if has_cycle(root, set()):としたほうが読み手にとって優しいと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 二重否定が認知負荷を上げるというのは覚えておきます。step2_recursion.pyを書き換えました。 |
||
| 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,24 @@ | ||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
|
|
||
| checked: set[int] = set() | ||
| for root in range(numCourses): | ||
| if root in checked: | ||
| continue | ||
|
|
||
| stack = [(root, set())] | ||
|
|
||
| while stack: | ||
| node, seen = stack.pop() | ||
| if node in seen: | ||
| return False | ||
| if node in checked: | ||
| continue | ||
| checked.add(node) | ||
| for child in adjacent_list[node]: | ||
| stack.append((child, seen | {node})) | ||
|
|
||
| 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,20 @@ | ||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| order = [0] * numCourses | ||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
| order[a] += 1 | ||
|
|
||
| nodes_with_no_parent = [node for node in range(numCourses) if order[node] == 0] | ||
|
|
||
| while nodes_with_no_parent: | ||
| next_nodes_with_no_parent = [] | ||
| for node in nodes_with_no_parent: | ||
| for child in adjacent_list[node]: | ||
| order[child] -= 1 | ||
| if order[child] == 0: | ||
| next_nodes_with_no_parent.append(child) | ||
| nodes_with_no_parent = next_nodes_with_no_parent | ||
|
|
||
| return sum(order) == 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,25 @@ | ||
| from collections import deque | ||
|
|
||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| indegree = [0] * numCourses | ||
|
|
||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
| indegree[a] += 1 | ||
|
|
||
| frontier = deque(node for node in range(numCourses) if indegree[node] == 0) | ||
| visited_count = 0 | ||
|
|
||
| while frontier: | ||
| node = frontier.popleft() | ||
| visited_count += 1 | ||
|
|
||
| for child in adjacent_list[node]: | ||
| indegree[child] -= 1 | ||
| if indegree[child] == 0: | ||
| frontier.append(child) | ||
|
|
||
| return visited_count == 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,36 @@ | ||
| NOT_VISITED = 0 | ||
| VISITING = 1 | ||
| VISITED = 2 | ||
|
|
||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
|
|
||
| status = [0] * numCourses | ||
| for root in range(numCourses): | ||
| if status[root] == VISITED: | ||
| continue | ||
|
|
||
| stack = [(root, False)] | ||
|
|
||
| while stack: | ||
| node, is_visited = stack.pop() | ||
|
|
||
| if is_visited: | ||
| status[node] = VISITED | ||
| continue | ||
|
|
||
| if status[node] == VISITING: | ||
| return False | ||
| if status[node] == VISITED: | ||
| continue | ||
|
|
||
| status[node] = VISITING | ||
| stack.append((node, True)) | ||
| for child in adjacent_list[node]: | ||
| stack.append((child, 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,34 @@ | ||
| NOT_VISITED = 0 | ||
| VISITING = 1 | ||
| VISITED = 2 | ||
|
|
||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
|
|
||
| status = [NOT_VISITED] * numCourses | ||
|
|
||
| def no_cycle(node): | ||
| if status[node] == VISITING: | ||
| return False | ||
|
|
||
| if status[node] == VISITED: | ||
| return True | ||
|
|
||
| status[node] = VISITING | ||
|
|
||
| for child in adjacent_list[node]: | ||
| if not no_cycle(child): | ||
| return False | ||
|
|
||
| status[node] = VISITED | ||
| return True | ||
|
|
||
| for root in range(numCourses): | ||
| if not no_cycle(root): | ||
| 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,34 @@ | ||
| NOT_VISITED = 0 | ||
| VISITING = 1 | ||
| VISITED = 2 | ||
|
|
||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| for a, b in prerequisites: | ||
| adjacent_list[b].append(a) | ||
|
|
||
| status = [NOT_VISITED] * numCourses | ||
|
|
||
| def has_cycle(node): | ||
| if status[node] == VISITING: | ||
| return True | ||
|
|
||
| if status[node] == VISITED: | ||
| return False | ||
|
|
||
| status[node] = VISITING | ||
|
|
||
| for child in adjacent_list[node]: | ||
| if has_cycle(child): | ||
| return True | ||
|
|
||
| status[node] = VISITED | ||
| return False | ||
|
|
||
| for root in range(numCourses): | ||
| if has_cycle(root): | ||
| return False | ||
|
|
||
| return True |
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.
閉路検出という言い方のほうが一般的かもしれません。