-
Notifications
You must be signed in to change notification settings - Fork 0
207. Course Schedule #3
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # | ||
| # @lc app=leetcode id=207 lang=python3 | ||
| # | ||
| # [207] Course Schedule | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
| constraint_map, num_of_constraint_map = dict(), dict() | ||
| for after, before in prerequisites: | ||
| if before not in constraint_map: | ||
| constraint_map[before] = [] | ||
| constraint_map[before].append(after) | ||
| if after not in num_of_constraint_map: | ||
| num_of_constraint_map[after] = 0 | ||
| num_of_constraint_map[after] += 1 | ||
|
|
||
| no_constraint_courses = [] | ||
| for i in range(numCourses): | ||
| if i not in num_of_constraint_map: | ||
| no_constraint_courses.append(i) | ||
|
|
||
| taking_order = [] | ||
| while no_constraint_courses: | ||
| course = no_constraint_courses.pop() | ||
| taking_order.append(course) | ||
| if course not in constraint_map: | ||
| continue | ||
| for constraint in constraint_map[course]: | ||
| num_of_constraint_map[constraint] -= 1 | ||
| if num_of_constraint_map[constraint] == 0: | ||
| no_constraint_courses.append(constraint) | ||
| return len(taking_order) == numCourses | ||
| # @lc code=end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # | ||
| # @lc app=leetcode id=207 lang=python3 | ||
| # | ||
| # [207] Course Schedule | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
| postrequisites_of_courses = [[] for _ in range(numCourses)] | ||
| num_prerequisites_of_courses = [0 for _ in range(numCourses)] | ||
|
|
||
| for course, prerequisite in prerequisites: | ||
| postrequisites_of_courses[prerequisite].append(course) | ||
| num_prerequisites_of_courses[course] += 1 | ||
|
|
||
| independent_courses = [] | ||
| for i in range(numCourses): | ||
| if num_prerequisites_of_courses[i] == 0: | ||
| independent_courses.append(i) | ||
|
|
||
| num_of_processed_courses = 0 | ||
| while independent_courses: | ||
| ic = independent_courses.pop() | ||
| for postrequisites in postrequisites_of_courses[ic]: | ||
| num_prerequisites_of_courses[postrequisites] -= 1 | ||
| if num_prerequisites_of_courses[postrequisites] == 0: | ||
| independent_courses.append(postrequisites) | ||
| num_of_processed_courses += 1 | ||
| return num_of_processed_courses == numCourses | ||
| # @lc code=end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # | ||
| # @lc app=leetcode id=207 lang=python3 | ||
| # | ||
| # [207] Course Schedule | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
|
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. こちらに書いてある深さ優先探索版を想定していました。
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. すみません、勘違いしていました。ちょっとリンクを参考に書き直してみます。 |
||
| def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
| postrequisites_of_courses = [[] for _ in range(numCourses)] | ||
| for course, prerequisite in prerequisites: | ||
| postrequisites_of_courses[prerequisite].append(course) | ||
|
|
||
| NOT_CHECKED, CHECKING, COMPLETED = 0, 1, 2 | ||
| def hasCycle(labels: List[int], course: int) -> bool: | ||
| if labels[course] == CHECKING: | ||
| return True | ||
| if labels[course] == COMPLETED: | ||
| return False | ||
| labels[course] = CHECKING | ||
| for post in postrequisites_of_courses[course]: | ||
| if hasCycle(labels, post): | ||
| return True | ||
| labels[course] = COMPLETED | ||
| return False | ||
|
|
||
| labels = [NOT_CHECKED for _ in range(numCourses)] | ||
| for course in range(numCourses): | ||
| if hasCycle(labels, course): | ||
| return False | ||
| return True | ||
| # @lc code=end | ||
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.
コードの読みやすさの観点からは、わざわざ 2 つの変数を同時に代入するメリットはないように思います。ただし、 2 つの変数をスワップする場合は、 2 つの変数を同時に代入する書き方のほうが分かりやすいと思います。
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.
コメントありがとうございます。たしかにそうですね、「なんとなくまとめられそうだから」でこのようにしていましたが、ただの代入はそれぞれで実行するようにしようと思います。