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
22 changes: 22 additions & 0 deletions non-overlapping-intervals/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
// Time O(n log n)
// Space O(n)
func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int {
if intervals.count < 2 { return 0 }

var answer = 0
let intervals = intervals.sorted { $0[1] < $1[1] }
var currentInterval = intervals[0]

for i in 1..<intervals.count {
Copy link
Contributor

Choose a reason for hiding this comment

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

1..<intervals.count 무슨 문법인가요? 잘몰라서..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

안녕하세요 주말에 감기몸살로 앓아서 늦게 답변드리는 점 양해부탁드립니다.
1부터 Intervals.count - 1까지 n번 반복한다는 의미입니다.
intervals.count가 5라고 한다면, 1~4까지 총 4번 반복합니다.

if currentInterval[1] <= intervals[i][0] {
currentInterval = intervals[i]
} else {
answer += 1
}
}

return answer
}
}

36 changes: 36 additions & 0 deletions remove-nth-node-from-end-of-list/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class ListNode {
public var val: Int
public var next: ListNode?
public init() { self.val = 0; self.next = nil; }
public init(_ val: Int) { self.val = val; self.next = nil; }
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
}

class Solution {
// Time O(n)
// Space O(1)
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
var slow: ListNode? = head
var fast: ListNode? = head
var prev: ListNode?

for _ in 1...n {
fast = fast?.next
}

while fast != nil {
prev = slow
slow = slow?.next
fast = fast?.next
}

if prev != nil {
prev?.next = slow?.next
} else {
return head?.next
}

return head
}
}

37 changes: 37 additions & 0 deletions same-tree/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 best O(log n)
// Space worst O(n)
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
return dfs(p, q)
}

private func dfs(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
if p == nil && q == nil {
return true
}

guard let p = p else { return false }
guard let q = q else { return false }

if p.val != q.val {
return false
}

return dfs(p.left, q.left) && dfs(p.right, q.right)
}
}