Skip to content

Commit 138d4bb

Browse files
committed
solve problem
1 parent 9b2ab2d commit 138d4bb

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

course-schedule/delight010.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
// Time O(V + E)
3+
// Space O(V + E)
4+
func canFinish(_ numCourses: Int, _ prerequisites: [[Int]]) -> Bool {
5+
var indegree: [Int] = Array(repeating: 0, count: numCourses)
6+
var queue: [Int] = []
7+
var graph: [[Int]] = Array(repeating: [], count: numCourses)
8+
9+
for i in 0..<prerequisites.count {
10+
let degree = prerequisites[i].first!
11+
let graphIndex = prerequisites[i][1]
12+
indegree[degree] += 1
13+
graph[graphIndex].append(degree)
14+
}
15+
16+
for i in 0..<indegree.count {
17+
if indegree[i] == 0 {
18+
queue.append(i)
19+
}
20+
}
21+
22+
var count = 0
23+
24+
while !queue.isEmpty {
25+
let current = queue.removeFirst()
26+
count += 1
27+
for i in graph[current] {
28+
indegree[i] -= 1
29+
if indegree[i] == 0 {
30+
queue.append(i)
31+
}
32+
}
33+
}
34+
35+
return count == numCourses
36+
}
37+
}
38+

jump-game/delight010.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
// Time O(n)
3+
// Space O(1)
4+
func canJump(_ nums: [Int]) -> Bool {
5+
var maxReachIndex = 0
6+
for i in 0..<nums.count {
7+
if i > maxReachIndex {
8+
return false
9+
}
10+
maxReachIndex = max(maxReachIndex, nums[i] + i)
11+
}
12+
13+
return maxReachIndex >= nums.count - 1
14+
}
15+
}
16+

0 commit comments

Comments
 (0)