일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 밴픽
- attribute
- concurrency
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- Class
- shiba
- 시바견
- Decorator
- Protocol
- 프로그래머스
- Python
- LeetCode
- Generator
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 715. Range Module
- 컴퓨터의 구조
- data science
- iterator
- 운영체제
- Convert Sorted List to Binary Search Tree
- Python Code
- Python Implementation
- Regular Expression
- Substring with Concatenation of All Words
- DWG
- 파이썬
- t1
- kaggle
Archives
- Today
- Total
Scribbling
LeetCode: 210. Course Schedule II 본문
위상 정렬 문제다.
result의 길이를 통해 위상 정렬 성공 여부를 파악 가능하다.
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
indegrees = [0] * numCourses
graph = [[] for _ in range(numCourses)]
for v, u in prerequisites:
graph[u].append(v)
indegrees[v] += 1
ret = []
q = []
for node, indegree in enumerate(indegrees):
if indegree == 0:
q.append(node)
while q:
u = q.pop()
ret.append(u)
for v in graph[u]:
indegrees[v] -= 1
if indegrees[v] == 0:
q.append(v)
return ret if len(ret) == numCourses else []
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 136. Single Number (0) | 2021.12.09 |
---|---|
LeetCode: 212. Word Search II (0) | 2021.12.08 |
LeetCode: 1011. Capacity To Ship Packages Within D Days (0) | 2021.12.06 |
LeetCode: 204. Count Primes (0) | 2021.12.05 |
LeetCode: 198. House Robber (0) | 2021.12.04 |