-
Notifications
You must be signed in to change notification settings - Fork 0
Course Schedule Ii #146
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
210.Course-Schedule-II
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 Ii #146
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,20 @@ | ||
| # 210. Course Schedule II | ||
|
|
||
| ## step1 | ||
| 最初集合で先祖を管理しようとしたが、閉路検出ができないことに気づき、statusを使う方針に書き換えた。 | ||
| 19mほど。 | ||
|
|
||
| ## 他の人のコードなど | ||
|
|
||
| > DFSのトポロジカルソートですが、preorder(行きがけ)でTEMPORARYとマークか、すでにTEMPORARYなら、サイクルと判定。postorder(帰りがけ)でPERMANENTとマークするみたいな感じですね。 | ||
|
|
||
| https://github.com/potrue/leetcode/pull/77/changes | ||
|
|
||
| 入次数で管理するやり方。Kahnのアルゴリズムというものだった。他の問題で見た覚えがあるが使えなかった。 | ||
|
|
||
| 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 | ||
|
|
||
| Kahnのアルゴリズムを書くのはBFS, DFSどちらでも可能だがBFSで実装する。 | ||
|
|
||
| ## step2 | ||
| 変数名を具体的にする |
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 @@ | ||
| class Solution: | ||
| def findOrder(self, numCourses: int, prerequisites: list[list[int]]) -> list[int]: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| for second, first in prerequisites: | ||
| adjacent_list[first].append(second) | ||
|
|
||
| order = [] | ||
| # 0: 未訪問, 1: 訪問中, 2: 訪問ずみ | ||
| status = [0] * numCourses | ||
|
|
||
| def dfs(i): | ||
|
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. 関数名は通常英単語の原形または命令形、もしくは助動詞などで始まることが多いと思います。また、関数名にはその関数が何をするのか、何が返ってくるのかを表す英単語・英語句を付けることをお勧めいたします。 dfs は実装の詳細に深入りしすぎているように感じます。 visit_course や can_finish_from などはいかがでしょうか? |
||
| if status[i] == 1: | ||
| return False | ||
| if status[i] == 2: | ||
| return True | ||
|
|
||
| status[i] = 1 | ||
| for j in adjacent_list[i]: | ||
| can_be_finished = dfs(j) | ||
| if not can_be_finished: | ||
| return False | ||
|
|
||
| status[i] = 2 | ||
| order.append(i) | ||
| return True | ||
|
|
||
| for i in range(numCourses): | ||
| if status[i] != 0: | ||
| continue | ||
| can_be_finished = dfs(i) | ||
| if not can_be_finished: | ||
| return [] | ||
|
|
||
| return order[::-1] | ||
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,40 @@ | ||
| from enum import IntEnum | ||
|
|
||
| class CourseStatus(IntEnum): | ||
| NOT_VISITED = 0 | ||
| VISITING = 1 | ||
| VISITED = 2 | ||
|
|
||
| class Solution: | ||
| def findOrder(self, numCourses: int, prerequisites: list[list[int]]) -> list[int]: | ||
| next_courses = [[] for _ in range(numCourses)] | ||
| for second, first in prerequisites: | ||
| next_courses[first].append(second) | ||
|
|
||
| order = [] | ||
| status = [CourseStatus.NOT_VISITED] * numCourses | ||
|
|
||
| def visit_course(course): | ||
| if status[course] == CourseStatus.VISITING: | ||
| return False | ||
| if status[course] == CourseStatus.VISITED: | ||
| return True | ||
|
|
||
| status[course] = CourseStatus.VISITING | ||
| for next_course in next_courses[course]: | ||
| can_be_finished = visit_course(next_course) | ||
| if not can_be_finished: | ||
| return False | ||
|
|
||
| status[course] = CourseStatus.VISITED | ||
| order.append(course) | ||
| return True | ||
|
|
||
| for course in range(numCourses): | ||
| if status[course] != CourseStatus.NOT_VISITED: | ||
| continue | ||
| can_be_finished = visit_course(course) | ||
| if not can_be_finished: | ||
| return [] | ||
|
|
||
| return order[::-1] |
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,29 @@ | ||
| import collections | ||
|
|
||
| class Solution: | ||
| def findOrder(self, numCourses: int, prerequisites: list[list[int]]) -> list[int]: | ||
| adjacent_list = [[] for _ in range(numCourses)] | ||
| in_degree = [0] * numCourses | ||
| for second, first in prerequisites: | ||
| adjacent_list[first].append(second) | ||
| in_degree[second] += 1 | ||
|
|
||
| queue = collections.deque() | ||
| for i in range(numCourses): | ||
| if in_degree[i] == 0: | ||
| queue.append(i) | ||
|
|
||
| order = [] | ||
| while queue: | ||
| i = queue.popleft() | ||
| order.append(i) | ||
| for j in adjacent_list[i]: | ||
| in_degree[j] -= 1 | ||
| if in_degree[j] == 0: | ||
| queue.append(j) | ||
|
|
||
| if len(order) != numCourses: | ||
| return [] | ||
| return order | ||
|
|
||
|
|
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,29 @@ | ||
| import collections | ||
|
|
||
| class Solution: | ||
| def findOrder(self, numCourses: int, prerequisites: list[list[int]]) -> list[int]: | ||
| next_courses = [[] for _ in range(numCourses)] | ||
| in_degree = [0] * numCourses | ||
| for second, first in prerequisites: | ||
| next_courses[first].append(second) | ||
| in_degree[second] += 1 | ||
|
|
||
| available_courses = collections.deque() | ||
| for course in range(numCourses): | ||
| if in_degree[course] == 0: | ||
| available_courses.append(course) | ||
|
|
||
| order = [] | ||
| while available_courses: | ||
| course = available_courses.popleft() | ||
| order.append(course) | ||
| for next_course in next_courses[course]: | ||
| in_degree[next_course] -= 1 | ||
| if in_degree[next_course] == 0: | ||
| available_courses.append(next_course) | ||
|
|
||
| if len(order) != numCourses: | ||
| return [] | ||
| return order | ||
|
|
||
|
|
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.
マジックナンバーとなっているのが気になりました。 enum の仕様をお勧めいたします。
https://docs.python.org/ja/3/library/enum.html