Skip to content

[HISEHOONAN] Week 04 Solutions #1353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2025
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
31 changes: 31 additions & 0 deletions find-minimum-in-rotated-sorted-array/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//그냥 정렬 하고 0번째 인덱스 원소 추출
//O(nlogn)
// class Solution {
// func findMin(_ nums: [Int]) -> Int {
// var sortednums = nums.sorted()
// return sortednums[0]
// }
// }


//이진 탐색
//배열이 다 정렬이 되어야 가능하지 않나? 했는데
//이 문제는 '회전된 정렬 배열'이라서 이진 탐색이 가능하다캄.
//O(logn)
class Solution {
func findMin(_ nums: [Int]) -> Int {
var left = 0
var right = nums.count - 1

while left < right{
var mid = (left+right)/2
if nums[mid] > nums[right]{
left = mid + 1
}else{
right = mid
}
}

return nums[left]
}
}
32 changes: 32 additions & 0 deletions maximum-depth-of-binary-tree/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Merge_Two_Sorted_Lists.swift
// Algorithm
//
// Created by 안세훈 on 4/22/25.
//

/**
* Definition for a binary tree node.
* 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 {
func maxDepth(_ root: TreeNode?) -> Int {
guard root != nil else { return 0 } //nil일 경우 깊이 0으로 처리

var leftdepth = maxDepth(root?.left) //왼쪽 노드의 깊이 탐색
var rightdepth = maxDepth(root?.right) //오른쪽 노드의 깊이 탐색

return max(leftdepth, rightdepth) + 1 //더 깊은 곳과 자기 자신을 위해 + 1을 해서 리턴.
}
}
48 changes: 48 additions & 0 deletions merge-two-sorted-lists/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Merge_Two_Sorted_Lists.swift
// Algorithm
//
// Created by 안세훈 on 4/22/25.
//

/**
* Definition for singly-linked list.
* 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 {
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
//list1, list2가 nil일 경우를 대비해 전처리
guard list1 != nil else { return list2 }
guard list2 != nil else { return list1 }

//초기화 Node
var head : ListNode? = ListNode(0)

//연산을 위한 Node
var current = head
var l1 = list1
var l2 = list2

while l1 != nil && l2 != nil{ //두 리스트 모두 nil이 아닐 경우에만 연산.
if let val1 = l1?.val, let val2 = l2?.val{ //비교할 node를 각각 list1?.val, list2?.val로 안전히 바인딩
if val1 < val2{ //두 수 비교
current?.next = ListNode(val1) //reusltNode의 다음을 작은수로 설정
l1 = l1?.next //연산 후 다음 노드로 넘김
}else{
current?.next = ListNode(val2) //reusltNode의 다음을 작은수로 설정
l2 = l2?.next //연산 후 다음 노드로 넘김
}
current = current?.next //연산 후 결과 노드도 다음으로 넘김
}
}
current?.next = l1 ?? l2 //while이 끝난 후 남은 노드는 뒤에 붙힘

return head?.next
}
}