From 1170faf7802592adbea65bb8741f02e4c9f48d5c Mon Sep 17 00:00:00 2001 From: xqm32 <458173774@qq.com> Date: Thu, 17 Aug 2023 21:07:16 +0800 Subject: [PATCH] leetcode: finished #210 --- content/leetcode/2023/8.md | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/content/leetcode/2023/8.md b/content/leetcode/2023/8.md index 20d0c8a15..90d1c9684 100644 --- a/content/leetcode/2023/8.md +++ b/content/leetcode/2023/8.md @@ -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