Skip to content

Commit a2a59d6

Browse files
authored
Merge pull request #1936 from sonjh1217/main
[sonjh1217] Week 12 solution
2 parents e24e48b + f444988 commit a2a59d6

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
/// 구간을 다루는데, 겹치지 않게 최대/최소로 뽑는 문제 -> 끝점 기준 정렬 + 그리디
3+
/// 가장 빨리 끝나는 것들을 선택해야 최대한 많이 선택할 수 있음
4+
/// 끝점 기준 오름차순 정렬 nlogn
5+
/// 순차적으로 돌면서 겹치면 제거(앞 것들보다 끝점이 더 뒤라서, 시작만 확인하면 됨)
6+
/// O(nlogn) time / O(n) space
7+
func eraseOverlapIntervals(_ intervals: [[Int]]) -> Int {
8+
let intervals = intervals.sorted { $0[1] < $1[1] }
9+
var end = intervals[0][1]
10+
var removal = 0
11+
12+
for i in 1..<intervals.count {
13+
let isOverlapping = intervals[i][0] < end
14+
if isOverlapping {
15+
removal += 1
16+
} else {
17+
end = intervals[i][1]
18+
}
19+
}
20+
21+
return removal
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
// O(n+m) (m = edges.count) time / O(n+m) space
3+
func countComponents(_ n: Int, _ edges: [[Int]]) -> Int {
4+
var graph = [[Int]](repeating: [], count: n)
5+
for edge in edges {
6+
graph[edge[0]].append(edge[1])
7+
graph[edge[1]].append(edge[0])
8+
}
9+
10+
var visited = [Bool](repeating: false, count: n)
11+
var connectedComponents = 0
12+
13+
for i in 0..<n {
14+
if visited[i] {
15+
continue
16+
}
17+
var queue = [Int]()
18+
queue.append(i)
19+
visited[i] = true
20+
var head = 0
21+
22+
while queue.count > head {
23+
let current = queue[head]
24+
head += 1
25+
26+
for node in graph[current] {
27+
if !visited[node] {
28+
visited[node] = true
29+
queue.append(node)
30+
}
31+
}
32+
}
33+
connectedComponents += 1
34+
}
35+
36+
return connectedComponents
37+
}
38+
}
39+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init() { self.val = 0; self.next = nil; }
7+
* public init(_ val: Int) { self.val = val; self.next = nil; }
8+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
// O(n) time / O(n) space
13+
func removeNthFromEndList(_ head: ListNode?, _ n: Int) -> ListNode? {
14+
var nodes = [ListNode]()
15+
var node = head
16+
while let current = node {
17+
nodes.append(current)
18+
node = current.next
19+
}
20+
21+
let indexToRemove = nodes.count - n
22+
23+
if indexToRemove == 0 {
24+
return head?.next
25+
}
26+
27+
nodes[indexToRemove - 1].next = indexToRemove + 1 < nodes.count ? nodes[indexToRemove + 1] : nil
28+
return head
29+
}
30+
31+
// O(n) time / O(1) space
32+
func removeNthFromEndPointer(_ head: ListNode?, _ n: Int) -> ListNode? {
33+
let dummy = ListNode(0)
34+
dummy.next = head
35+
var post: ListNode? = dummy
36+
var prev: ListNode? = dummy
37+
38+
for _ in 0...n {
39+
post = post?.next
40+
}
41+
42+
while post != nil {
43+
prev = prev?.next
44+
post = post?.next
45+
}
46+
47+
prev?.next = prev?.next?.next
48+
return dummy.next
49+
}
50+
}

same-tree/sonjh1217.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
* self.val = val
11+
* self.left = left
12+
* self.right = right
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
// O(n) time, O(h) space h = 트리의 높이. 최악: n, 평균: log n
18+
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
19+
switch (p, q) {
20+
case (nil, nil):
21+
return true
22+
case let (p?, q?):
23+
return p.val == q.val
24+
&& isSameTree(p.left, q.left)
25+
&& isSameTree(p.right, q.right)
26+
default:
27+
return false
28+
}
29+
}
30+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init(_ val: Int) {
8+
* self.val = val
9+
* self.left = nil
10+
* self.right = nil
11+
* }
12+
* }
13+
*/
14+
15+
class Codec {
16+
// O(n) time / O(n) space
17+
func serialize(_ root: TreeNode?) -> String {
18+
var serializedNodes = [String]()
19+
var queueNodes = [TreeNode?]()
20+
queueNodes.append(root)
21+
var head = 0
22+
23+
while head < queueNodes.count {
24+
let node = queueNodes[head]
25+
head += 1
26+
27+
if let node {
28+
serializedNodes.append("\(node.val)")
29+
30+
queueNodes.append(node.left)
31+
queueNodes.append(node.right)
32+
} else {
33+
serializedNodes.append("null")
34+
}
35+
36+
}
37+
38+
return serializedNodes.joined(separator: ",")
39+
}
40+
41+
// O(n) time / O(n) space
42+
func deserialize(_ data: String) -> TreeNode? {
43+
let serializedTree = data
44+
var nodeVals = serializedTree.split(separator: ",")
45+
46+
guard let first = nodeVals.first,
47+
let firstVal = Int(first) else {
48+
return nil
49+
}
50+
51+
let root = TreeNode(firstVal)
52+
var queueNodes = [TreeNode]()
53+
queueNodes.append(root)
54+
var head = 0
55+
var isLeft = true
56+
57+
for i in 1..<nodeVals.count {
58+
let node = queueNodes[head]
59+
let stringValue = nodeVals[i]
60+
let val = Int(stringValue)
61+
62+
if isLeft {
63+
if let val {
64+
let leftNode = TreeNode(val)
65+
node.left = leftNode
66+
queueNodes.append(leftNode)
67+
} else {
68+
node.left = nil
69+
}
70+
} else {
71+
if let val {
72+
let rightNode = TreeNode(val)
73+
node.right = rightNode
74+
queueNodes.append(rightNode)
75+
} else {
76+
node.right = nil
77+
}
78+
head += 1
79+
}
80+
isLeft.toggle()
81+
}
82+
83+
return root
84+
}
85+
}
86+
87+

0 commit comments

Comments
 (0)