File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // 207. Course Schedule
3
+ // https://leetcode.com/problems/course-schedule/description/
4
+ // Dale-Study
5
+ //
6
+ // Created by WhiteHyun on 2024/06/30.
7
+ //
8
+
9
+ class Solution {
10
+ func canFinish( _ numCourses: Int , _ prerequisites: [ [ Int ] ] ) -> Bool {
11
+ var graph : [ Int : [ Int ] ] = [ : ]
12
+ var visited : [ Int : Bool ] = [ : ]
13
+
14
+ // 그래프 구성
15
+ for prereq in prerequisites {
16
+ let course = prereq [ 0 ]
17
+ let prerequisite = prereq [ 1 ]
18
+ graph [ course, default: [ ] ] . append ( prerequisite)
19
+ }
20
+
21
+ func dfs( _ course: Int ) -> Bool {
22
+ if visited [ course] == true { return false }
23
+ if visited [ course] == false { return true }
24
+
25
+ visited [ course] = true
26
+
27
+ if let prerequisites = graph [ course] {
28
+ for prereq in prerequisites {
29
+ if !dfs( prereq) { return false }
30
+ }
31
+ }
32
+
33
+ visited [ course] = false
34
+ return true
35
+ }
36
+
37
+ for course in 0 ..< numCourses {
38
+ if !dfs( course) { return false }
39
+ }
40
+
41
+ return true
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments