Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ LeetCode の問題を以下手順で解く

| # | Title | Language | Difficulty |
| --- | ----------------------------------------------------------------------------------------------------- | -------- | ---------- |
| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/description/) | Python3 | Medium |
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | Python3 | Medium |
| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | Python3 | Medium |

Expand Down
35 changes: 35 additions & 0 deletions problems/medium/python3/207/course-schedule.step1.py
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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コードの読みやすさの観点からは、わざわざ 2 つの変数を同時に代入するメリットはないように思います。ただし、 2 つの変数をスワップする場合は、 2 つの変数を同時に代入する書き方のほうが分かりやすいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。たしかにそうですね、「なんとなくまとめられそうだから」でこのようにしていましたが、ただの代入はそれぞれで実行するようにしようと思います。

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
31 changes: 31 additions & 0 deletions problems/medium/python3/207/course-schedule.step2.py
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
32 changes: 32 additions & 0 deletions problems/medium/python3/207/course-schedule.step3.py
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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらに書いてある深さ優先探索版を想定していました。
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
こちらのやり方で書いてみていただけますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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