Skip to content

Commit 4102a3e

Browse files
authored
Merge pull request #2147 from acious/main
[acious] WEEK 04 solutions
2 parents 7c95f89 + ec2f957 commit 4102a3e

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

combination-sum/acious.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
3+
val result = mutableListOf<List<Int>>()
4+
val current = mutableListOf<Int>()
5+
6+
fun dfs(start: Int, total: Int) {
7+
if (total > target) return
8+
9+
if (total == target) {
10+
result.add(ArrayList(current))
11+
return
12+
}
13+
14+
for (i in start until candidates.size) {
15+
val num = candidates[i]
16+
current.add(num)
17+
dfs(i, total + num)
18+
current.removeAt(current.size - 1)
19+
}
20+
}
21+
22+
dfs(0, 0)
23+
return result
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
fun findMin(nums: IntArray): Int {
3+
var low = 1
4+
var high = nums.size - 1
5+
6+
while (low <= high) {
7+
// 코틀린에서 Int끼리의 나눗셈은 자동으로 소수점을 버립니다 (Floor)
8+
val mid = (low + high) / 2
9+
10+
// 회전된 지점(변곡점)을 찾은 경우: 바로 리턴
11+
if (nums[mid - 1] > nums[mid]) {
12+
return nums[mid]
13+
}
14+
15+
// 왼쪽 부분이 정렬되어 있다면, 최소값은 오른쪽에 있음
16+
if (nums[0] < nums[mid]) {
17+
low = mid + 1
18+
} else {
19+
// 오른쪽 부분이 정렬되어 있다면(혹은 변곡점이 왼쪽에 있다면), 왼쪽으로 이동
20+
high = mid - 1
21+
}
22+
}
23+
24+
// 반복문이 끝날 때까지 못 찾았거나, 배열이 회전되지 않은 경우 첫 번째 요소가 최소값
25+
return nums[0]
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
class Solution {
12+
fun maxDepth(root: TreeNode?): Int {
13+
if (root == null) return 0
14+
15+
// 굳이 Nullable을 담을 필요 없으므로 Queue<TreeNode> 사용
16+
val queue: Queue<TreeNode> = LinkedList()
17+
queue.add(root)
18+
19+
var maxDepth = 0
20+
21+
while (queue.isNotEmpty()) {
22+
val levelSize = queue.size // 현재 레벨에 있는 노드의 개수
23+
maxDepth++ // 레벨 진입 시 깊이 1 증가
24+
25+
// 현재 레벨에 있는 노드들을 몽땅 꺼내고, 다음 레벨(자식들)을 넣음
26+
repeat(levelSize) {
27+
val curNode = queue.poll() // LinkedList에서는 remove()보다 poll()이 안전
28+
29+
// 자식 노드가 있으면 큐에 추가 (다음 레벨 예비군)
30+
curNode.left?.let { queue.add(it) }
31+
curNode.right?.let { queue.add(it) }
32+
}
33+
}
34+
35+
return maxDepth
36+
}
37+
}

merge-two-sorted-lists/acious.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
3+
var curOneNode = list1
4+
var curTwoNode = list2
5+
6+
val resultStartNode = ListNode(999)
7+
var resultCurNode = resultStartNode
8+
9+
while (curOneNode != null && curTwoNode != null) {
10+
11+
if (curOneNode.`val` <= curTwoNode.`val`) {
12+
resultCurNode.next = curOneNode
13+
curOneNode = curOneNode.next
14+
} else {
15+
resultCurNode.next = curTwoNode
16+
curTwoNode = curTwoNode.next
17+
}
18+
19+
resultCurNode = resultCurNode.next!!
20+
}
21+
22+
if (curOneNode != null) {
23+
resultCurNode.next = curOneNode
24+
}
25+
if (curTwoNode != null) {
26+
resultCurNode.next = curTwoNode
27+
}
28+
29+
return resultStartNode.next
30+
}
31+
}

0 commit comments

Comments
 (0)