Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions course-schedule/delight010.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift 코드는 처음보는데 저랑 같은 방식으로 풀이를 해주셔서 이해하는데 어렵지 않았습니다. 시간 복잡도와 공간 복잡도도 따로 작성해주셨네요. 항상 작성하시는 건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

가급적이면 작성하려고 해요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
// Time O(V + E)
// Space O(V + E)
func canFinish(_ numCourses: Int, _ prerequisites: [[Int]]) -> Bool {
var indegree: [Int] = Array(repeating: 0, count: numCourses)
var queue: [Int] = []
var graph: [[Int]] = Array(repeating: [], count: numCourses)

for i in 0..<prerequisites.count {
let degree = prerequisites[i].first!
let graphIndex = prerequisites[i][1]
indegree[degree] += 1
graph[graphIndex].append(degree)
}

for i in 0..<indegree.count {
if indegree[i] == 0 {
queue.append(i)
}
}

var count = 0

while !queue.isEmpty {
let current = queue.removeFirst()
count += 1
for i in graph[current] {
indegree[i] -= 1
if indegree[i] == 0 {
queue.append(i)
}
}
}

return count == numCourses
}
}

41 changes: 41 additions & 0 deletions invert-binary-tree/delight010.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저와 다른 풀이를 볼 수 있었습니다. 덕분에 BFS 방식으로도 풀이할 수 있다는 것을 알게 됐습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init( val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init( val: Int, * left: TreeNode?, * right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}

class Solution {
// Time O(n)
// Space O(n)
func invertTree(_ root: TreeNode?) -> TreeNode? {
guard let root = root else {
return nil
}

var queue: [TreeNode] = [root]

while queue.isEmpty == false {
let current = queue.removeFirst()

if let left = current.left {
queue.append(left)
}

if let right = current.right {
queue.append(right)
}

(current.left, current.right) = (current.right, current.left)
}

return root
}
}

16 changes: 16 additions & 0 deletions jump-game/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
// Time O(n)
// Space O(1)
func canJump(_ nums: [Int]) -> Bool {
var maxReachIndex = 0
for i in 0..<nums.count {
if i > maxReachIndex {
return false
}
maxReachIndex = max(maxReachIndex, nums[i] + i)
}

return maxReachIndex >= nums.count - 1
}
}