Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 26 additions & 0 deletions find-minimum-in-rotated-sorted-array/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
func findMin(_ nums: [Int]) -> Int {
var low = 1
var high = nums.count - 1

while low <= high {
let mid = (low + high) / 2
Copy link
Member

Choose a reason for hiding this comment

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

디스코드에 Tony님이 올려주신 mid 오버플로우 관련 글도 한 번 읽어보시면 좋을 듯 합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

감사합니다! 덕분에 Swift overflow한도도 배우고, (64라서 2^(63) - 1 ≈ 9.22 × 10^18라고 합니다)
코드도 overflow 방지하는 스타일로 변경했습니다. 095850e


if nums[mid] < nums[mid - 1] {
return nums[mid]
}

if nums[mid] > nums[0] {
low = mid + 1
} else {
high = mid - 1
}
}

return nums[0]

//시간복잡도 O(logn)
//공간복잡도 O(1)
}
}

42 changes: 42 additions & 0 deletions maximum-depth-of-binary-tree/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
func maxDepthStack(_ root: TreeNode?) -> Int {
guard let root = root else {
return 0
}
var searchStack = [(TreeNode, Int)]()
searchStack.append((root, 1))
var maxDepth = 1

while searchStack.isEmpty == false {
guard let popped = searchStack.popLast() else {
break
}
maxDepth = max(popped.1, maxDepth)

if let left = popped.0.left {
searchStack.append((left, popped.1 + 1))
}

if let right = popped.0.right {
searchStack.append((right, popped.1 + 1))
}
}

return maxDepth

//시간복잡도 O(n)
//공간복잡도 O(n)
}

func maxDepthRecursion(_ root: TreeNode?) -> Int {
guard let root = root else {
return 0
}

return max(maxDepth(root.left), maxDepth(root.right)) + 1

//시간복잡도 O(n)
//공간복잡도 O(n)
}
}

56 changes: 56 additions & 0 deletions merge-two-sorted-lists/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Solution {
func mergeTwoListsIterative(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
var iterList1 = list1
var iterList2 = list2

var dummy = ListNode()
var merged = dummy

while iterList1 != nil && iterList2 != nil,
let list1 = iterList1,
let list2 = iterList2 {
if list1.val < list2.val {
merged.next = list1
iterList1 = list1.next
} else {
merged.next = list2
iterList2 = list2.next
}

guard let next = merged.next else {
break
}

merged = next
}

merged.next = iterList1 ?? iterList2

return dummy.next

//시간복잡도 O(m+n)
//공간복잡도 O(1)
}

func mergeTwoListsRecursion(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
guard var list1 = list1 else {
return list2
}

guard var list2 = list2 else {
return list1
}

if list1.val < list2.val {
list1.next = mergeTwoListsRecursion(list1.next, list2)
return list1
} else {
list2.next = mergeTwoListsRecursion(list1, list2.next)
return list2
}

//시간 복잡도 O(m+n)
//공간 복잡도 O(m+n)
}
}