Skip to content

Commit 2fd52b4

Browse files
authored
Merge pull request #1825 from sonjh1217/main
[sonjh1217] WEEK 04 solutions
2 parents 0aa2539 + 095850e commit 2fd52b4

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func findMin(_ nums: [Int]) -> Int {
3+
var low = 1
4+
var high = nums.count - 1
5+
6+
while low <= high {
7+
let mid = low + (high - low) / 2
8+
9+
if nums[mid] < nums[mid - 1] {
10+
return nums[mid]
11+
}
12+
13+
if nums[mid] > nums[0] {
14+
low = mid + 1
15+
} else {
16+
high = mid - 1
17+
}
18+
}
19+
20+
return nums[0]
21+
22+
//시간복잡도 O(logn)
23+
//공간복잡도 O(1)
24+
}
25+
}
26+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
func maxDepthStack(_ root: TreeNode?) -> Int {
3+
guard let root = root else {
4+
return 0
5+
}
6+
var searchStack = [(TreeNode, Int)]()
7+
searchStack.append((root, 1))
8+
var maxDepth = 1
9+
10+
while searchStack.isEmpty == false {
11+
guard let popped = searchStack.popLast() else {
12+
break
13+
}
14+
maxDepth = max(popped.1, maxDepth)
15+
16+
if let left = popped.0.left {
17+
searchStack.append((left, popped.1 + 1))
18+
}
19+
20+
if let right = popped.0.right {
21+
searchStack.append((right, popped.1 + 1))
22+
}
23+
}
24+
25+
return maxDepth
26+
27+
//시간복잡도 O(n)
28+
//공간복잡도 O(n)
29+
}
30+
31+
func maxDepthRecursion(_ root: TreeNode?) -> Int {
32+
guard let root = root else {
33+
return 0
34+
}
35+
36+
return max(maxDepth(root.left), maxDepth(root.right)) + 1
37+
38+
//시간복잡도 O(n)
39+
//공간복잡도 O(n)
40+
}
41+
}
42+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
func mergeTwoListsIterative(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
3+
var iterList1 = list1
4+
var iterList2 = list2
5+
6+
var dummy = ListNode()
7+
var merged = dummy
8+
9+
while iterList1 != nil && iterList2 != nil,
10+
let list1 = iterList1,
11+
let list2 = iterList2 {
12+
if list1.val < list2.val {
13+
merged.next = list1
14+
iterList1 = list1.next
15+
} else {
16+
merged.next = list2
17+
iterList2 = list2.next
18+
}
19+
20+
guard let next = merged.next else {
21+
break
22+
}
23+
24+
merged = next
25+
}
26+
27+
merged.next = iterList1 ?? iterList2
28+
29+
return dummy.next
30+
31+
//시간복잡도 O(m+n)
32+
//공간복잡도 O(1)
33+
}
34+
35+
func mergeTwoListsRecursion(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
36+
guard var list1 = list1 else {
37+
return list2
38+
}
39+
40+
guard var list2 = list2 else {
41+
return list1
42+
}
43+
44+
if list1.val < list2.val {
45+
list1.next = mergeTwoListsRecursion(list1.next, list2)
46+
return list1
47+
} else {
48+
list2.next = mergeTwoListsRecursion(list1, list2.next)
49+
return list2
50+
}
51+
52+
//시간 복잡도 O(m+n)
53+
//공간 복잡도 O(m+n)
54+
}
55+
}
56+

0 commit comments

Comments
 (0)