Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #210
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Aug 17, 2023
1 parent a03291a commit 1170faf
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions content/leetcode/2023/8.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ title: "2023.8"
draft: false
---

# 2023.8.17

```python
#
# @lc app=leetcode.cn id=210 lang=python3
#
# [210] 课程表 II
#

# @lc code=start
from typing import List


class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
# 0: unvisited, 1: visiting, 2: visited
visited = [0] * numCourses
# graph[i]: list of courses that require course i
graph = [[] for _ in range(numCourses)]
for x, y in prerequisites:
graph[x].append(y)

ans = []

def dfs(i: int) -> bool:
if visited[i] == 1:
return False
if visited[i] == 2:
return True
visited[i] = 1
for j in graph[i]:
if not dfs(j):
return False
visited[i] = 2
ans.append(i)
return True

for i in range(numCourses):
if not dfs(i):
return []
return ans


# @lc code=end
```

# 2023.8.16

```python
Expand Down

0 comments on commit 1170faf

Please sign in to comment.