[LeetCode][Python3] 207. Course Schedule
2019. 3. 31. 00:55 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/course-schedule/
My Solution :
class Solution:
def canFinish(self, numCourses, prerequisites):
def dfs(vertex):
if visited[vertex]:
return True
if visited[vertex] == False:
return False
visited[vertex] = False
for prerequisite in graph[vertex]:
if not dfs(prerequisite):
return False
visited[vertex] = True
return True
visited = [None]*numCourses
graph = [list() for _ in range(numCourses)]
for vertex, prerequisite in prerequisites:
graph[vertex].append(prerequisite)
for vertex in range(numCourses):
if not dfs(vertex):
return False
return True
Comment :
전형적인 그래프 탐색 문제인데 볼 때마다 새롭고 한 번에 잘 안 풀리네 ㅠㅠ
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 139. Word Break (0) | 2019.04.03 |
---|---|
[LeetCode][Python3] 56. Merge Intervals (0) | 2019.04.03 |
[LeetCode][Python3] 295. Find Median from Data Stream (0) | 2019.04.02 |
[LeetCode][Python3] 236. Lowest Common Ancestor of a Binary Tree (0) | 2019.04.01 |
[LeetCode][Python3] 116. Populating Next Right Pointers in Each Node (0) | 2019.03.30 |
[LeetCode][Python3] 208. Implement Trie (Prefix Tree) (1) | 2019.03.30 |
[LeetCode][Python3] 239. Sliding Window Maximum (0) | 2019.03.29 |
[LeetCode][Python3] 315. Count of Smaller Numbers After Self (0) | 2019.03.28 |
최근에 달린 댓글 최근에 달린 댓글