diff --git a/combination-sum/acious.kt b/combination-sum/acious.kt new file mode 100644 index 0000000000..9619b4a3fa --- /dev/null +++ b/combination-sum/acious.kt @@ -0,0 +1,25 @@ +class Solution { + fun combinationSum(candidates: IntArray, target: Int): List> { + val result = mutableListOf>() + val current = mutableListOf() + + fun dfs(start: Int, total: Int) { + if (total > target) return + + if (total == target) { + result.add(ArrayList(current)) + return + } + + for (i in start until candidates.size) { + val num = candidates[i] + current.add(num) + dfs(i, total + num) + current.removeAt(current.size - 1) + } + } + + dfs(0, 0) + return result + } +} diff --git a/find-minimum-in-rotated-sorted-array/acious.kt b/find-minimum-in-rotated-sorted-array/acious.kt new file mode 100644 index 0000000000..b79aa63ee9 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/acious.kt @@ -0,0 +1,27 @@ +class Solution { + fun findMin(nums: IntArray): Int { + var low = 1 + var high = nums.size - 1 + + while (low <= high) { + // 코틀린에서 Int끼리의 나눗셈은 자동으로 소수점을 버립니다 (Floor) + val mid = (low + high) / 2 + + // 회전된 지점(변곡점)을 찾은 경우: 바로 리턴 + if (nums[mid - 1] > nums[mid]) { + return nums[mid] + } + + // 왼쪽 부분이 정렬되어 있다면, 최소값은 오른쪽에 있음 + if (nums[0] < nums[mid]) { + low = mid + 1 + } else { + // 오른쪽 부분이 정렬되어 있다면(혹은 변곡점이 왼쪽에 있다면), 왼쪽으로 이동 + high = mid - 1 + } + } + + // 반복문이 끝날 때까지 못 찾았거나, 배열이 회전되지 않은 경우 첫 번째 요소가 최소값 + return nums[0] + } +} diff --git a/maximum-depth-of-binary-tree/acious.kt b/maximum-depth-of-binary-tree/acious.kt new file mode 100644 index 0000000000..fe8903e185 --- /dev/null +++ b/maximum-depth-of-binary-tree/acious.kt @@ -0,0 +1,37 @@ +/** + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ +class Solution { + fun maxDepth(root: TreeNode?): Int { + if (root == null) return 0 + + // 굳이 Nullable을 담을 필요 없으므로 Queue 사용 + val queue: Queue = LinkedList() + queue.add(root) + + var maxDepth = 0 + + while (queue.isNotEmpty()) { + val levelSize = queue.size // 현재 레벨에 있는 노드의 개수 + maxDepth++ // 레벨 진입 시 깊이 1 증가 + + // 현재 레벨에 있는 노드들을 몽땅 꺼내고, 다음 레벨(자식들)을 넣음 + repeat(levelSize) { + val curNode = queue.poll() // LinkedList에서는 remove()보다 poll()이 안전 + + // 자식 노드가 있으면 큐에 추가 (다음 레벨 예비군) + curNode.left?.let { queue.add(it) } + curNode.right?.let { queue.add(it) } + } + } + + return maxDepth + } +} diff --git a/merge-two-sorted-lists/acious.kt b/merge-two-sorted-lists/acious.kt new file mode 100644 index 0000000000..35750c5aa6 --- /dev/null +++ b/merge-two-sorted-lists/acious.kt @@ -0,0 +1,31 @@ +class Solution { + fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? { + var curOneNode = list1 + var curTwoNode = list2 + + val resultStartNode = ListNode(999) + var resultCurNode = resultStartNode + + while (curOneNode != null && curTwoNode != null) { + + if (curOneNode.`val` <= curTwoNode.`val`) { + resultCurNode.next = curOneNode + curOneNode = curOneNode.next + } else { + resultCurNode.next = curTwoNode + curTwoNode = curTwoNode.next + } + + resultCurNode = resultCurNode.next!! + } + + if (curOneNode != null) { + resultCurNode.next = curOneNode + } + if (curTwoNode != null) { + resultCurNode.next = curTwoNode + } + + return resultStartNode.next + } +}